0
votes

I have a user model with an email and password. I added the fields first_name and last_name to my database and the form for the add view like this:

     9  <div class="users form large-12 medium-9 columns">
     10     <?= $this->Form->create($user) ?>
     11     <fieldset>
     12         <legend><?= __('New Account') ?></legend>
     13         <?php
     14             echo $this->Form->input('email');
     15             echo $this->Form->input('first_name');
     16             echo $this->Form->input('last_name');
     17             echo $this->Form->input('password');
     18
     19         ?>
     20     </fieldset>
     21     <?= $this->Form->button(__('Submit')) ?>
     22     <?= $this->Form->end() ?>
     23 </div>                            

The email and password save without issue, but first_name and last_name never do. This is the controller function. Adding the commented line causes the first_name field to save, but it seems pretty clear that I shouldn't have to do that.

     46     public function add()
 47     {
 48         $user = $this->Users->newEntity();
 49         if ($this->request->is('post')) {
 50             $user = $this->Users->patchEntity($user, $this->request->data);
 51             //$user->first_name = $this->request->data['first_name'];
 52             if ($this->Users->save($user)) {
 53                 $this->Flash->success(__('The user has been saved.'));
 54                 return $this->redirect(['action' => 'index']);
 55             } else {
 56                 $this->Flash->error(__('The user could not be saved. Please, try again.'));
 57             }
 58         }
 59         $books = $this->Users->Books->find('list', ['limit' => 200]);
 60         $this->set(compact('user', 'books'));
 61         $this->set('_serialize', ['user']);
 62     }

Does anyone know why this is happening? I tried clearing the model cache but nothing changed.

Thanks!

2
Set debug to 1 or 2 in your core.php file with Configure::write('debug', 2);Holt
@Holt What core.php file? I have an /config/app.php file, but as far as I can tell debug can be set only to true or false, and it is set to true.xPfqHZ
Did you update the entity?Guillermo Mansilla
@xPfqHZ Sorry, I misread your title, though you were using CakePHP 2.0.Holt
The issue was white-listing the new properties for mass assignment book.cakephp.org/3.0/en/orm/entities.html#mass-assignmentxPfqHZ

2 Answers

0
votes

To mass-assign new properties using

$this->Model->patchEntity($entity, $this->request->data);

you have to whitelist them. In this case, in the /src/Model/Entity/User.php file:

 1     protected $_accessible = [
 2         'email' => true,
 3         'password' => true,
 4         'first_name' => true, //add this
 5         'last_name' => true,  //add this
 6     ];

On the other hand, directly assigning properties (as in $user->first_name = $this->request->data['first_name'];) is always possible.

More info: http://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment

0
votes

Another way is setting the accassible fields in the creation of the entity.

$user = $this->Users->newEntity($this->request->data, [
    'accessibleFields' => [
          'email' => true,
          'password' => true,
          'first_name' => true,
          'last_name' => true, //or you could just use '*' => true
    ] 
]);

There is also no need to call newEntity and then patchEntity with the data, you can give the data to newEntity in the first place as in my example