是否可以在实体侦听器中保留实体?我正在尝试实现审计跟踪功能:
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尚未保留在数据库中。
答案 0 :(得分:0)
您可以在onFlush
事件中插入其他实体,您可以在http://www.doctrine-project.org/blog/doctrine2-versionable