我可以在CakePHP的update语句中添加一个条件吗? [英] Can I add a condition to CakePHP's update statement?
问题描述
由于似乎没有任何支援 CakePHP中的乐观锁定,我正在建立一个实现它的行为。在对行为进行一点研究之后,我想我可以在beforeSave事件中运行一个查询来检查版本字段是否没有改变。
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.
但是,我宁愿通过从
WHERE id = ?
到
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.
我可以看到 DboSource.update()
方法支持条件,但 Model.save()
从不传递任何条件。
I can see that the DboSource.update()
method supports conditions, but Model.save()
never passes any conditions to it.
有几个选项:
- 执行
beforeSave()
- 修改CakePHP的本地副本,以检查
中的
条件
(DboSource.update()$>)中的
options
数组c $ c>方法。
- Do the check in
beforeSave()
and live with the fact that it's not bulletproof. - Hack my local copy of CakePHP to check for a
conditions
key in theoptions
array ofModel.save()
and pass it along to theDboSource.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?
推荐答案
我去了我的本地副本CakePHP。我最近没有看到,看看CakePHP的最新版本是否提供任何新的选项。
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.
这篇关于我可以在CakePHP的update语句中添加一个条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!