Reputation: 1428
I'm relatively new with CakePHP and i'm trying to save some related data.
Right now I have an messages database and an users database and I'd like to save the user that send the message in the users table, but this users could already be there if they send multiple messages.
Right now I'm updating like this:
$this->Message->saveAll(array(
'Message' => array(
'contents' => $contents,
),
'User' => array(
'name' => $name,
'email' => $email,
)
);
However this causes mysql to throw an error because CakePHP tries to insert the user as if it is new, but the email already exists and should be unique.
Is there any way to do this? Or should I check if the user already exists myself?
TIA.
Upvotes: 1
Views: 2627
Reputation: 3415
Do you have an 'id'
field in the users table?
If so, this is what CakePHP will use to determine whether it should be updating or inserting a row.
So you're best off doing a find from User
first to seed the data you want to update User with, at least to get the 'id'
. If you don't supply an 'id'
CakePHP will try to insert.
Upvotes: 3