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.