I have a problem with creating a HABTM record in my CakePHP application. Basically, I have objects that a user can like, exactly the same as Facebook. The three tables are:
users
objects
objects_users
I’m trying to create a HABTM record with the following code:
$data = array(
'Object' => array(
'id' => $object_id
),
'User' => array(
'id' => $this->Auth->user('id')
)
);
$this->Object->User->save($data);
But I keep getting the following error message:
Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
dbprefix
.objects_users
, CONSTRAINTobjects_users_ibfk_2
FOREIGN KEY (user_id
) REFERENCESusers
(id
) ON DELETE CASCADE ON UPDATE CASCADE)
How do I create a HABTM record programatically in my controller when I know the two relavant IDs?