From fe48a6d5584667fd38984d9ea27b09c3a6797866 Mon Sep 17 00:00:00 2001 From: Gerhard Seidel Date: Thu, 21 Aug 2025 15:05:51 +0200 Subject: [PATCH 1/2] fix: compute changes on lately added entities for insertions --- src/UnitOfWork.php | 10 +++++++--- .../ORM/Functional/PrePersistEventTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index b76872092b1..831ce2b5a17 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,8 @@ public function computeChangeSets() } } } + + $this->computeScheduleInsertsChangeSets(); } /** diff --git a/tests/Tests/ORM/Functional/PrePersistEventTest.php b/tests/Tests/ORM/Functional/PrePersistEventTest.php index 58f551bdedd..ee672311f4f 100644 --- a/tests/Tests/ORM/Functional/PrePersistEventTest.php +++ b/tests/Tests/ORM/Functional/PrePersistEventTest.php @@ -40,6 +40,25 @@ 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(); } } From 0b5096024708c7704174722d8573ffee69dc4071 Mon Sep 17 00:00:00 2001 From: Gerhard Seidel Date: Thu, 21 Aug 2025 16:27:46 +0200 Subject: [PATCH 2/2] fix: cs --- src/UnitOfWork.php | 3 ++- tests/Tests/ORM/Functional/PrePersistEventTest.php | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index 831ce2b5a17..716bac631d6 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -552,7 +552,7 @@ private function computeScheduleInsertsChangeSets(): void { foreach ($this->entityInsertions as $entity) { $oid = spl_object_id($entity); - if (!isset($this->entityChangeSets[$oid])) { + if (! isset($this->entityChangeSets[$oid])) { $class = $this->em->getClassMetadata(get_class($entity)); $this->computeChangeSet($class, $entity); } @@ -932,6 +932,7 @@ 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 ee672311f4f..9ab7ecf5505 100644 --- a/tests/Tests/ORM/Functional/PrePersistEventTest.php +++ b/tests/Tests/ORM/Functional/PrePersistEventTest.php @@ -48,17 +48,19 @@ public function testPersistEntityOnFlushWithPrePersistHook(): void { $this->_em->getEventManager()->addEventListener(Events::prePersist, new PrePersistUnmappedPersistListener()); - $alreadyPersistedEntity = new EntityWithCascadeAssociation(); + $alreadyPersistedEntity = new EntityWithCascadeAssociation(); $this->_em->persist($alreadyPersistedEntity); $this->_em->flush(); $entityWithUnmapped = new EntityWithUnmappedEntity(); $entityWithCascade = new EntityWithCascadeAssociation(); - $entityWithUnmapped->unmapped = $entityWithCascade; + $entityWithUnmapped->unmapped = $entityWithCascade; $alreadyPersistedEntity->cascaded = $entityWithUnmapped; $this->_em->flush(); + + $this->assertTrue($this->_em->getUnitOfWork()->isInIdentityMap($entityWithCascade)); } }