Tuesday, June 23, 2015

Cakephp 3 ignores new field when adding new record (Bookmarker tutorial, bake, patchEntity())

Hey,

I've been using CakePHP since the 1.2 version and have used all major releases before in client projects. Now I'm testing the new version (3.0.6) and did that by using the "Bookmarker"-Tutorial.

So I added a few things and modified everything to my needs and it's working quite well, but when I try to extend an enitty with a new field, Cake simply ignores it when I want to add a new record.

The scenario is:

A new user record shall be added, and the user-table contains the new field "gender". This shall be an INT value (1, 2 or 3) which is connected to a simple select within the user-create form (class is set because I'm using Bootstrap 3 for layout):


    // src/Templates/User/add.ctp
    echo $this
->Form->input('gender', array(
       
'class' => 'form-control',
       
'empty' => __('Select gender'),
       
'options' => $genders
   
));


In the table (and in the UserEntity.php) I added the field, within the entity I added:

    // src/Model/Entity/User.php
   
protected $_accessible = array(
       
'email' => true,
       
'gender' => true,
       
'password' => true,
       
'bookmarks' => true,
   
);


The controller takes the posted form-data and I do a standard "patchEntity" before I try to save the record (code is completely created by the bake-shell):

    // src/Controller/UsersController.php
   
public function add() {
       
        $user
= $this->Users->newEntity();
       
if($this->request->is('post')) {
            $user
= $this->Users->patchEntity($user, $this->request->data);
           
if($this->Users->save($user)) {
               
....
           
}
       
}
   
}

Just before running into saving the record, the entity contains the selected gender-value (pr()-output of the patched entity):

    App\Model\Entity\User Object (
       
[_accessible:protected] => Array (
           
[email] => 1
           
[gender] => 1
           
[password] => 1
           
[bookmarks] => 1 )
       
[_properties:protected] => Array (
           
[gender] => 1
           
[email] => mail@mail.com
           
[password] => $2y$10$FV.....
       
)
       
...
   
)


But the value "1" for the gender will be saved as "0" in the database...

I am clearly missing something here, so I would really appreciate if someone could point me in the right direction. It is a bit of work to get comfortable with the new ORM and other changes from Cake 2 to version 3, but it's getting better and better each day working with it. Great job evolving the framework, it feels good to see how far Cake has come in the last few years!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: