From a10e50554b1439253e7af4730b6024228fefa7be Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 15 Nov 2026 18:20:45 +0000 Subject: [PATCH] Clear partial-load tracking when detaching or deleting entities UnitOfWork::$partialObjectLoadedFields is keyed by spl_object_id(), but was not cleaned up on detach() or after a deletion was flushed. Once the object was released, PHP could reuse its object id for a new entity, which then inherited the stale entry and got spurious updates scheduled. Fixes #12784 Co-Authored-By: Claude Opus 5.5 --- src/UnitOfWork.php | 2 + .../ORM/Functional/Ticket/GH12784Test.php | 102 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 tests/Tests/ORM/Functional/Ticket/GH12784Test.php diff --git a/src/UnitOfWork.php b/src/UnitOfWork.php index 340e212b9ea..df19714e2bb 100644 --- a/src/UnitOfWork.php +++ b/src/UnitOfWork.php @@ -1321,6 +1321,7 @@ private function executeDeletions(): void $this->entityIdentifiers[$oid], $this->originalEntityData[$oid], $this->entityStates[$oid], + $this->partialObjectLoadedFields[$oid], ); // Entity with this $oid after deletion treated as NEW, even if the $oid @@ -2131,6 +2132,7 @@ private function doDetach( $this->entityIdentifiers[$oid], $this->entityStates[$oid], $this->originalEntityData[$oid], + $this->partialObjectLoadedFields[$oid], ); break; case self::STATE_NEW: diff --git a/tests/Tests/ORM/Functional/Ticket/GH12784Test.php b/tests/Tests/ORM/Functional/Ticket/GH12784Test.php new file mode 100644 index 00000000000..0735f4fde8a --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/GH12784Test.php @@ -0,0 +1,102 @@ +=8.4.0')] +class GH12784Test extends OrmFunctionalTestCase +{ + protected function setUp(): void + { + parent::setUp(); + + if (! $this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) { + $this->markTestSkipped('Test requires native lazy objects enabled.'); + } + + $this->createSchemaForModels(GH12784Entity::class); + } + + public function testDetachedProxyDoesNotLeakPartialLoadStateToReusedObjectId(): void + { + $id = $this->createRow(); + + $proxy = $this->_em->getReference(GH12784Entity::class, $id); + $objectId = spl_object_id($proxy); + + $this->_em->detach($proxy); + unset($proxy); + + $this->assertNoUpdateForNewEntityWithObjectId($objectId); + } + + public function testRemovedProxyDoesNotLeakPartialLoadStateToReusedObjectId(): void + { + $id = $this->createRow(); + + $proxy = $this->_em->getReference(GH12784Entity::class, $id); + $objectId = spl_object_id($proxy); + + $this->_em->remove($proxy); + $this->_em->flush(); + unset($proxy); + + $this->assertNoUpdateForNewEntityWithObjectId($objectId); + } + + private function createRow(): int + { + $entity = new GH12784Entity(); + $this->_em->persist($entity); + $this->_em->flush(); + $this->_em->clear(); + + return $entity->id; + } + + private function assertNoUpdateForNewEntityWithObjectId(int $objectId): void + { + $held = []; + + for ($i = 0; $i < 1000; $i++) { + $entity = new GH12784Entity(); + + if (spl_object_id($entity) === $objectId) { + break; + } + + $held[] = $entity; + } + + self::assertSame($objectId, spl_object_id($entity), 'Could not reuse the object id of the released proxy.'); + + $this->_em->persist($entity); + $this->_em->flush(); + + $this->_em->getUnitOfWork()->computeChangeSets(); + + self::assertSame([], $this->_em->getUnitOfWork()->getEntityChangeSet($entity)); + self::assertFalse($this->_em->getUnitOfWork()->isScheduledForUpdate($entity)); + } +} + +#[ORM\Entity] +#[ORM\Table(name: 'gh12784_entity')] +class GH12784Entity +{ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column] + public int $id; + + #[ORM\Column] + public string $name = 'new'; +}