This has been causing me considerable grief for a couple of days now, so I turn to the community for help.
Assume two tables - Album HABTM Customer The join table is customer_albums PK(album_id, customer_id)
I have two controller functions:
public function like($album_id) {
if ($this->request->is('post')) {
$this->Album->CustomerAlbum->save(array('album_id' => $album_id,'customer_id' => $this->Auth->user('id')));
$this->redirect(Controller::referer());
}
}
and this one...
public function unlike($album_id) {
$this->log($album_id);
if ($this->request->is('post')) {
$this->Album->CustomerAlbum->deleteAll(array('album_id' => $album_id,'customer_id' => $this->Auth->user('id'),false));
$this->redirect(Controller::referer());
}
}
The "Like" function results in the SQL:
INSERT INTO gre49302_digital.customer_albums (album_id, customer_id) VALUES (1, 62)
which is what I would expect.
However, the "Unlike" function results in:
SELECT CustomerAlbum.album_id FROM gre49302_digital.customer_albums AS CustomerAlbum WHERE album_id = 1 AND customer_id = 62
DELETE CustomerAlbum FROM gre49302_digital.customer_albums AS CustomerAlbum WHERE CustomerAlbum.album_id = (1)
Which indicates to me that CakePHP doesn't understand the concept of a compound primary key.
Consequently when attempting to delete one "like" between customer and album, I end up deleting everything for the selected album.
Ideally the "unlike" function should simply delete a single record from customer_albums using a compound primary key as selector.