diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index b76872092b..716bac631d 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -551,9 +551,11 @@ private function postCommitCleanup($entity): void private function computeScheduleInsertsChangeSets(): void { foreach ($this->entityInsertions as $entity) { - $class = $this->em->getClassMetadata(get_class($entity)); - - $this->computeChangeSet($class, $entity); + $oid = spl_object_id($entity); + if (! isset($this->entityChangeSets[$oid])) { + $class = $this->em->getClassMetadata(get_class($entity)); + $this->computeChangeSet($class, $entity); + } } } @@ -929,6 +931,9 @@ public function computeChangeSets() } } } + + // Check again for uncomputed changes that might be added by hooks + $this->computeScheduleInsertsChangeSets(); } /** diff --git a/tests/Tests/ORM/Functional/PrePersistEventTest.php b/tests/Tests/ORM/Functional/PrePersistEventTest.php index 58f551bded..9ab7ecf550 100644 --- a/tests/Tests/ORM/Functional/PrePersistEventTest.php +++ b/tests/Tests/ORM/Functional/PrePersistEventTest.php @@ -40,6 +40,27 @@ public function testCallingPersistInPrePersistHook(): void $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForInsert($entityWithCascade)); $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForInsert($entityWithUnmapped)); + + $this->_em->flush(); + } + + public function testPersistEntityOnFlushWithPrePersistHook(): void + { + $this->_em->getEventManager()->addEventListener(Events::prePersist, new PrePersistUnmappedPersistListener()); + + $alreadyPersistedEntity = new EntityWithCascadeAssociation(); + $this->_em->persist($alreadyPersistedEntity); + $this->_em->flush(); + + $entityWithUnmapped = new EntityWithUnmappedEntity(); + $entityWithCascade = new EntityWithCascadeAssociation(); + + $entityWithUnmapped->unmapped = $entityWithCascade; + $alreadyPersistedEntity->cascaded = $entityWithUnmapped; + + $this->_em->flush(); + + $this->assertTrue($this->_em->getUnitOfWork()->isInIdentityMap($entityWithCascade)); } }