开发者

Problem with updating record in database

开发者 https://www.devze.com 2023-03-11 18:11 出处:网络
I have problem to update user data in cakephp. When I submit form I have this data in $this->data: Array

I have problem to update user data in cakephp.

When I submit form I have this data in $this->data:

Array
(
    [User] => Array
        (
            [first_name] => Dusan
            [last_name] => Stojanovic
            [native_language_id] => 25
        )
)

but, when i try to update it with:

$this->User->id = $id;
$this->User-&g开发者_Python百科t;save($this->data)

model is not saved, because of validation. They try to validate field which don't exist in $this->data array (like invalid password, email etc.). How I can fix this so I can update only fields that exist in $this->data array?


To properly do this you should set the validation rules' on option to create only and/or set require to false.

For a quickfix, supply a $fieldlist of fields you want to save:

$this->User->save($this->data, true, array('first_name', 'last_name', 'native_language_id'));


you can do this:

$this->data['User']['id'] = $id

because of the ['id'] cakephp recognizes the array as data that will update a dataset and will not create a new data set.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号