如何覆盖 preUpdate 操作以不修改 Doctrine 中的“updated_at”字段?
我想禁用在更新对象时更改“updated_at”字段的自动行为。我想手动完成;或者至少可以根据需要禁用它。
我知道我可以通过构建自己的行为来做到这一点,如 这个很棒的答案 。但我正在寻找一些“更干净”的东西来修改对象的侦听器。
- 我尝试覆盖模型上的 preUpdate() 操作。
- 我尝试禁用侦听器,但没有任何效果:
--
Doctrine::getTable('Place')->getRecordListener()->setOption('disabled', true);
// or
Doctrine::getTable('Place')->getRecordListener()->setOption('disabled', array('preUpdate'));
// As reference, I've used these two lines on a Symfony Task
还有更多想法或代码可供查看吗?
I want to disable the automatic behaviour of changing the 'updated_at' field when an object is updated. I want to do it manually; or at least, have the posibility to disable it as wanted.
I know I can do this by building my own behaviour as in this great answer. But I was searching for something 'cleaner' modifying a listener of the object.
- I've tried to override the preUpdate() action on the model.
- I've tried to disable the listeners, and nothing:
--
Doctrine::getTable('Place')->getRecordListener()->setOption('disabled', true);
// or
Doctrine::getTable('Place')->getRecordListener()->setOption('disabled', array('preUpdate'));
// As reference, I've used these two lines on a Symfony Task
Any more ideas, or code to look at?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以直接从对象访问侦听器,如下所示:
You can access the listener directly from your object like this:
根据 http://www 上的文档.doctrine-project.org/documentation/manual/1_1/nl/behaviors:core-behaviors#timestampable 如果您想使用 Timestampable 但不使用它的更新部分,只需使用:
并添加您自己的 Updated_at 字段列部分。
according to the docs at http://www.doctrine-project.org/documentation/manual/1_1/nl/behaviors:core-behaviors#timestampable if you want to use Timestampable but not the updated portion of it just use:
And add in your own updated_at field in the columns section.
最简单的方法是将
updated_at
字段重命名为其他名称。以至于它被教义所忽视。这样,您就可以准确控制该字段的内容。
The most simple way to do this would be to rename the
updated_at
field to something else. So that it is ignored by Doctrine.That way, you can control the contents of the field exactly.
问题是您必须知道侦听器的位置,
您也可以通过这种方式禁用所有侦听器:
problem is that you have to know the position of the listener
you can also disable all listeners this way: