Karena ketika onFlush
dipanggil, semua perubahan sudah dihitung dan Anda perlu menyegarkannya jika Anda mengubah entitas atau membuat entitas baru.
$em = $this->getEntityManager();
$uow = $em->getUnitOfWork();
$logMetadata = $em->getClassMetadata('Your\LogClass');
...
$em->persist($log);
$uow->computeChangeSet($logMetadata, $log);
Untuk 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);
}
}