几天来,这一直让我感到非常悲伤,所以我向社区寻求帮助。
假设有两个表 - Album HABTM Customer 连接表是 customer_albums PK(album_id, customer_id)
我有两个控制器功能:
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());
}
}
和这个...
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());
}
}
“Like”函数生成 SQL:
INSERT INTO gre49302_digital.customer_albums (album_id, customer_id) VALUES (1, 62)
这是我所期望的。
但是,“Unlike”函数会导致:
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)
这向我表明 CakePHP 不理解复合主键的概念。
因此,当试图删除客户和专辑之间的“喜欢”时,我最终会删除所选专辑的所有内容。
理想情况下,“unlike”函数应该使用复合主键作为选择器从 customer_albums 中简单地删除一条记录。