16

How to you extend a cakePHP project so it can use a new field in the database?

I just given a CakePHP Project that I am trying to extend the model to include a new field. I The original Developer is no longer available, and I haven't worked with CakePHP previously. The problem is that all of the other fields are being saved correctly, but the new field is being saved as an empty string.

The database has been extended to include the new field:

class_time  varchar(30)

I extended the original view to support the new field

<?=$form->input('release', array('type' => 'radio', 'legend' => false, 'div' => 'radio', 'options' => array('Agree' => 'Agree &nbsp;', 'Disagree' => 'Disagree')))?>
<?=$form->input('class_time', array('type' => 'radio', 'legend' => false, 'div' => 'radio', 'options' => array('No preference' => 'No preference&nbsp;', '6:00-8:30 P.M. ' => '6:00-8:30 P.M. ', '6:30-9:00 P.M.' => '6:30-9:00 P.M.')))?>

As near as I can tell, the page is rendering the HTML correctly

<div class="radio"><input type="hidden" name="data[Account][release]" id="AccountRelease_" value=""><input type="radio" name="data[Account][release]" id="AccountReleaseAgree" value="Agree"><label for="AccountReleaseAgree">Agree &nbsp;</label><input type="radio" name="data[Account][release]" id="AccountReleaseDisagree" value="Disagree"><label for="AccountReleaseDisagree">Disagree</label></div>
<div class="radio"><input type="hidden" name="data[Account][class_time]" id="AccountClassTime_" value=""><input type="radio" name="data[Account][class_time]" id="AccountClassTimeNoPreference" value="No preference"><label for="AccountClassTimeNoPreference">No preference&nbsp;</label><input type="radio" name="data[Account][class_time]" id="AccountClassTime6:00-8:30P.m." value="6:00-8:30 P.M. "><label for="AccountClassTime6:00-8:30P.m.">6:00-8:30 P.M. </label><input type="radio" name="data[Account][class_time]" id="AccountClassTime6:30-9:00P.m." value="6:30-9:00 P.M."><label for="AccountClassTime6:30-9:00P.m.">6:30-9:00 P.M.</label></div>

But when it saves, it is saving the selection for the "release" field (and the others), but not the class_time.

From what I can find in the cakePHP documentation, app/models/account.php is where I believe I would need to define the new field, but it only consists of the following:

<?php
  class Account extends AppModel {
    var $name = 'Account';
  }
?>

Which makes me wonder how the original developer got the "release" to save, even though it doesn't appear to be defined.

Is there something that I am missing, or that still needs to be done?

1
  • There is a Controller action between the view and the model. The problem is probably there. The controller is probably in app/controllers/accounts_controller.php, try to locate the action code inside it and post it here.
    – bfavaretto
    Aug 19, 2011 at 4:53

10 Answers 10

61

Whenever you make any changes to your database, please make sure that your app/config/core.php file debug value is 2. Configure::write('debug', 2);

If it is 0 database changes will not be detected.

7
  • That feature seems a little cryptic (imho), but it updated it just like you said... Thanks for your quick response!
    – Tezyn
    Aug 19, 2011 at 5:22
  • 4
    FWIW this is because in production Cake caches the database structure so that it doesn't have to query the db again at every page load. This is rarely a problem because you should develop with debug level set to 2 anyway.
    – JJJ
    Aug 19, 2011 at 5:52
  • 6
    I recommend manually deleting the temp cache files on a live website as opposed to making it a habit to turn on debug mode whenever there is a database change. Turning on debug mode presents detailed errors along with database queries, pr() etc, which is not for public's eyes. Refer to answer at stackoverflow.com/a/21779427/216084
    – user216084
    Mar 28, 2014 at 10:36
  • 3
    I think other answer are better, because if you have running your website you do not want to show debug information to your users. Better is "sudo rm app/tmp/cahe/models/*"
    – inye
    May 17, 2014 at 16:10
  • app/config/core.php no longer exists in CakePhp3. So, I would suggest this answer stackoverflow.com/a/21779427/8719007. Works perfectly Jul 23, 2019 at 8:35
28

In CakePHP application, whenever you add a new field or modify the structure in database, you should delete all files inside YourApplication/app/tmp/cache/models folder.

1
  • Won't it affect anything on live environment?
    – Rajat
    Mar 27, 2021 at 15:37
5

Whenever you do any database change follow the following steps :

  1. Clear files under \app\tmp\cache\models
  2. If you are using any cache engines like memcache, restart the cache service.
  3. Set the debug to 2 in core.php
4

Quite a bit later, but I was looking for a way to do this today in Cake 3.x. Easiest way, imo:

bin/cake orm_cache build

which will rebuild the cache w/ the current db structure.

Or to just clear w/o rebuild:

bin/cake orm_cache clear

http://book.cakephp.org/3.0/en/console-and-shells/orm-cache.html

2
  • This is the recommended approach, according to the docs. More here: book.cakephp.org/3.0/en/…
    – ericgio
    Mar 20, 2017 at 21:35
  • That's what fixed my issue. Thank you, @BSounder.
    – sk001
    Mar 10, 2021 at 22:37
3

Try to clear all models under cache directory.

app/tmp/cache/models

2

clear cache at /app/tmp/cache

if raising debug level to 2 or 3 is not working.

check if, on using debug($Model), shows your new field under schema attribute of model.

2

You can also delete cache file if your database changes are not working.

path of your application\app\tmp\cache\models

2

In cakephp 3 you should also check your entity file for that model, and make sure that the field is under the $_accesible property.

Example:

protected $_accessible = [
    'name' => true,
    'topic' => true,
    'new_field' => true
];
1
1

You need to inform cakephp that you have new fields in your db. Why you need to do so is coz cakephp scans the db only once and generates the schemas. Once refers to first time it's run. You can find the schemas in app\tmp\cache\models.

So if you clear the files in the above folder cake will re generate the schema. Now there is a exception to it, if you are in development/debug mode cakephp scans it everytime.

-2

Actually, any changes to database, anytime , you should change debug mode to 2 in app/config/core.php if it is zero. Otherwise it will take values from cache.

1
  • 2
    -1: Setting debug mode to non-zero clears the cache. Your solution will work, but your reasoning is wrong. Also, setting debug mode to a non-zero number is not recommended for live websites, because it will expose detailed error messages and database queries. For live websites see stackoverflow.com/a/21779427/216084
    – user216084
    Aug 11, 2014 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.