Thursday, June 28, 2012

Re: saveAssociated with hasOne creates new entries instead of updating

On Thu, Jun 28, 2012 at 11:43 AM, cronet <cronet@gmx.de> wrote:
> saveAssociated keeps generating new entries, rather than updating old ones:
>
> User hasOne UserProfile
> User hasOne UserSetting
>
> The resulting array looks like this:
>
> [User] => Array
>     (
>         [id] => 3
>     )
>
> [UserProfile] => Array
>     (
>         [firstname] => Test
>         [name] => Name
>     )
>
> [UserSetting] => Array
>     (
>         [some_setting] => Doh
>     )
>
>
> In my controller I simply call
>
> $this->User->saveAssociated($this->request->data);
>

In your controller before you call saveAssociated, set the values for
user_id in the UserProfile and UserSetting parts of the array.

$this->request->data['UserProfile']['user_id'] =
$this->request->data['User']['id'];
$this->request->data['UserSetting']['user_id'] =
$this->request->data['User']['id'];

Or if you want to use an interim array, assign the request data to an
array and then set the user_id. Then use that array to save the
associated data.

$request_data = $this->request->data;
$request_data['UserProfile']['user_id'] = $this->request->data['User']['id'];
$request_data['UserSetting']['user_id'] = $this->request->data['User']['id'];
$this->User->saveAssociated($request_data);

Mike.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: