PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] CakePHP 3 new fields won't save correctly

 January 18, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

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!


Solution

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



Answered By - xPfqHZ
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing