Can I add a condition to CakePHP's update statement?

夙愿已清 提交于 2019-12-06 08:16:10
deceze

When using save() to update a record, Cake expects an id to be present and will update only the record with this id.

What you're looking for is updateAll():

updateAll(array $fields, array $conditions)

Updates many records in a single call. Records to be updated are identified by the $conditions array, and fields to be updated, along with their values, are identified by the $fields array.

For example, to approve all bakers who have been members for over a year, the update call might look something like:

$this_year = date('Y-m-d h:i:s', strtotime('-1 year'));

$this->Baker->updateAll(
    array('Baker.approved' => true),
    array('Baker.created <=' => "$this_year")
);

I went with hacking my local copy of CakePHP. I haven't looked recently to see if the latest versions of CakePHP offer any new options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!