1

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($customerAuditTrail);
    }
  }
}

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

Jeboy
  • 228
  • 4
  • 14

1 Answers1

0

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

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Diego
  • 41
  • 1
  • 3