如何运行更新查询?

时间:2010-09-01 10:12:27

标签: cakephp

UPDATE图像SET is_primary = 0 WHERE event_id = $ id

来自事件控制器,其中Event hasMany Image。

2 个答案:

答案 0 :(得分:3)

$this->Event->saveAll($this->data);

请参阅:http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto

和:http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm

或者,您可以保存事件,然后循环并保存每个图像。

还有以下选项:

$this->Image->query("UPDATE images SET is_primary = 0 WHERE event_id = $id");

请参阅:http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-query

答案 1 :(得分:3)

$this->Event->Image->updateAll(array('is_primary'=>0), array('Event.id'=>$id));

Check this并搜索 updateAll