如何在实例侦听器中保留实体?

时间:2011-07-28 00:06:15

标签: doctrine-orm

是否可以在实体侦听器中保留实体?我正在尝试实现审计跟踪功能:

public function preUpdate($eventArgs)
{
  if ($eventArgs->getEntity() instanceof \Domain\Entity\Customer) {
    $customerAuditTrail = new \Domain\Entity\CustomerAuditTrail();

    if ($eventArgs->hasChangedField('FirstName')) {
      $customerAuditTrail->setAction('Update');
      $customerAuditTrail->setField('FirstName');
      $customerAuditTrail->setCustomer($eventArgs->getEntity());

      $customerAuditTrail->setOldValue($eventArgs->getOldValue('FirstName'));
      $customerAuditTrail->setNewValue($eventArgs->getNewValue('FirstName'));
      ...

      $this->_em->persist($customerAuditTrail);
    }
  }
}

保存操作正常,但customerAuditTrail尚未保留在数据库中。

1 个答案:

答案 0 :(得分:0)

您可以在onFlush事件中插入其他实体,您可以在http://www.doctrine-project.org/blog/doctrine2-versionable

中看到示例