如何覆盖preupdate操作以不修改我的“更新_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
还有更多的想法或代码要查看吗?
谢谢!
解决方案
根据文档 http://www.doctrine-project.org/documentation/manual/1_1/nl/behaviors:core-behaviors#timestampable 如果要使用可使用时间戳但不使用更新的部分,则只需使用:
Timestampable:
updated:
disabled: true
并在“列”部分中添加您自己的Updated_at字段。
其他提示
您可以这样直接从对象访问听众:
$listenerChain = $this->getListener();
$i = 0;
while ($listener = $listenerChain->get($i))
{
if ($listener instanceof Doctrine_Template_Listener_Timestampable)
{
$listener->setOption('disabled', true);
break;
}
$i++;
}
最简单的方法是重命名 updated_at
字段与其他事物。这样就被学说忽略了。
这样,您可以准确控制字段的内容。
// get the first (in our case the timstampable) listener for the record
$timestampable = $record->getListener()->get(0);
// disable setting of created_at at the timestampable listener
$timestampable->setOption(array('created' => array('disabled' => true)));
问题是您必须知道听众的位置
您也可以以这种方式禁用所有听众:
$record->getListener()->setOption('disabled',true);
不隶属于 StackOverflow