1

我想禁用在更新对象时更改“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

还有更多想法或要查看的代码吗?

谢谢!

4

4 回答 4

4

您可以像这样直接从您的对象访问侦听器:


  $listenerChain = $this->getListener();

  $i = 0;

  while ($listener = $listenerChain->get($i))
  {
    if ($listener instanceof Doctrine_Template_Listener_Timestampable)
    {
      $listener->setOption('disabled', true);
      break;
    }
    $i++;
  }     
于 2012-03-01T11:29:53.157 回答
3

根据http://www.doctrine-project.org/documentation/manual/1_1/nl/behaviors:core-behaviors#timestampable上的文档,如果您想使用 Timestampable 但不是它的更新部分,只需使用:

Timestampable:
  updated:
    disabled: true

并在列部分添加您自己的 updated_at 字段。

于 2010-02-20T00:18:14.997 回答
0

最简单的方法是将updated_at字段重命名为其他名称。所以它被Doctrine忽略了。

这样,您可以准确地控制字段的内容。

于 2010-02-19T09:18:13.317 回答
0
// 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);
于 2011-02-23T13:25:54.520 回答