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

让人想犯罪 __ 提交于 2019-12-08 02:22:13

问题


Since there doesn't seem to be any support for optimistic locking in CakePHP, I'm taking a stab at building a behaviour that implements it. After a little research into behaviours, I think I could run a query in the beforeSave event to check that the version field hasn't changed.

However, I'd rather implement the check by changing the update statement's WHERE clause from

WHERE id = ?

to

WHERE id = ? and version = ?

This way I don't have to worry about other requests changing the database record between the time I read the version and the time I execute the update. It also means I can do one database call instead of two.

I can see that the DboSource.update() method supports conditions, but Model.save() never passes any conditions to it.

It seems like I have a couple of options:

  1. Do the check in beforeSave() and live with the fact that it's not bulletproof.
  2. Hack my local copy of CakePHP to check for a conditions key in the options array of Model.save() and pass it along to the DboSource.update() method.

Right now, I'm leaning in favour of the second option, but that means I can't share my behaviour with other users unless they apply my hack to their framework.

Have I missed an easier option?


回答1:


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")
);



回答2:


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.



来源:https://stackoverflow.com/questions/1479961/can-i-add-a-condition-to-cakephps-update-statement

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