开发者

How to persist an entity inside an event listener?

Is it possible to persist an entity inside an event listener? I'm trying to implement an audit trail feature:

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($cus开发者_如何学PythontomerAuditTrail);
    }
  }
}

The save operation is working fine but the customerAuditTrail has not been persisted in the database.


You can insert other entities in onFlush event, you can see an example in http://www.doctrine-project.org/blog/doctrine2-versionable

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜