我有这个Topic表,该表链接到模型中具有hasMany的表Post。
这是我的$ this-> request-> data
Array
(
[Topic] => Array
(
[id] => 1
[topic_title] => This is my topic
)
[Post] => Array
(
[1] => Array
(
[id] => 1
[title] => Blah
[message] => My message
)
[2] => Array
(
[id] => 2
[title] => Second Blah
[message] => Second My message
)
)
)
我用它更新:
$this->Topic->saveAssociated($this->request->data);
但是,如果我还希望能够添加和删除该主题的帖子,该怎么办?
保持相同的结构,您可以执行a $this->Topic->saveAssociated($this->request->data);
,它将'id' => NULL
在数据数组中添加任何新的(或未设置的)项。
关于删除,我知道唯一会同时删除的情况是将HABTM标记为'unique' => true
。否则,您需要做一个$this->Post->deleteAll(array('Post.topic_id' => $unwanted_topic_id), false);
我可以考虑制作一个新数组,保留要删除的数组,并将其作为deleteAll函数的条件发送。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句