Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp new field not saving

Tags:

cakephp

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?

like image 609
Tezyn Avatar asked Aug 19 '11 04:08

Tezyn


4 Answers

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.

like image 51
Vins Avatar answered Nov 19 '22 21:11

Vins


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.

like image 42
Smruti Avatar answered Nov 19 '22 19:11

Smruti


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
like image 5
Guru Avatar answered Nov 19 '22 21:11

Guru


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

like image 4
BSounder Avatar answered Nov 19 '22 19:11

BSounder