简体   繁体   中英

CakePHP 3 new fields won't save correctly

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM