Fordi når onFlush
kaldes, er alle ændringer allerede beregnet, og du skal opdatere dem, hvis du ændrer din enhed eller opretter en ny enhed.
$em = $this->getEntityManager();
$uow = $em->getUnitOfWork();
$logMetadata = $em->getClassMetadata('Your\LogClass');
...
$em->persist($log);
$uow->computeChangeSet($logMetadata, $log);
For postPersist:
$em = $this->getEntityManager();
$uow = $em->getUnitOfWork();
$log = new Log;
...
$logMetadata = $em->getClassMetadata('Your\LogClass');
$className = $logMetadata->name;
$persister = $this->getEntityPersister($className);
$persister->addInsert($log);
$uow->computeChangeSet($classMeta, $logEntry);
$postInsertIds = $persister->executeInserts();
if ($postInsertIds) {
foreach ($postInsertIds as $id => $entity) {
$idField = $logMetadata->identifier[0];
$logMetadata->reflFields[$idField]->setValue($entity, $id);
$this->addToIdentityMap($entity);
}
}