From c05882a3ba35f8858de189aec0029b0598de7dce Mon Sep 17 00:00:00 2001 From: wickedOne Date: Mon, 18 Mar 2024 13:41:21 +0100 Subject: [PATCH] fix: clear entity persister on restoring class metadata (#264) restoring the class metadata in the metadata factory does not restore the one doctrine has cached in the `class` property of it's persisters.... downstream this might lead to unexpected behaviour: when the id generator has been replaced with this library's `IdGenerator`, the basic entity persister does not recognise it as an id generator and treats the identity field as one which can be used for inserts in the [`BasicEntityPersister::getInsertColumnList`](https://github.com/doctrine/orm/blob/94144e122779098c156098fc9bb6791e53192086/src/Persisters/Entity/BasicEntityPersister.php#L1443-L1451) method this can lead to `Invalid parameter number: number of bound variables does not match number of tokens` errors on subsequent inserts of the entity manager. this change will fix that --- .../Doctrine/Entity/DummyWithProperty.php | 30 +++++++++++++++++++ .../Persister/ObjectManagerPersister.php | 1 + .../Persister/ObjectManagerPersisterTest.php | 19 ++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 fixtures/Bridge/Doctrine/Entity/DummyWithProperty.php diff --git a/fixtures/Bridge/Doctrine/Entity/DummyWithProperty.php b/fixtures/Bridge/Doctrine/Entity/DummyWithProperty.php new file mode 100644 index 00000000..6ec0cc54 --- /dev/null +++ b/fixtures/Bridge/Doctrine/Entity/DummyWithProperty.php @@ -0,0 +1,30 @@ +metadataToRestore as $metadata) { $metadataFactory->setMetadataFor($metadata->getName(), $metadata); + $this->clearUnitOfWorkPersister($metadata->getName()); } $this->metadataToRestore = []; diff --git a/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php b/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php index ef10b37a..d602cf55 100644 --- a/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php +++ b/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php @@ -21,6 +21,7 @@ use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\DummySubClass; use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\DummyWithEmbeddable; use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\DummyWithIdentifier; +use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\DummyWithProperty; use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\DummyWithRelatedCascadePersist; use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\DummyWithRelation; use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\MappedSuperclassDummy; @@ -140,6 +141,24 @@ public function testCanPersistMultipleEntitiesWithExplicitIdentifierSet(): void self::assertInstanceOf(DummyWithIdentifier::class, $entity); } + public function testClearingUnitOfWorkPersister(): void + { + $dummy = new DummyWithProperty(); + $dummy->id = 1; + + $this->persister->persist($dummy); + $this->persister->flush(); + + $dummy = new DummyWithProperty(); + $dummy->property = 'foo'; + + $this->entityManager->persist($dummy); + $this->entityManager->flush(); + + $entity = $this->entityManager->getRepository(DummyWithProperty::class)->findOneBy(['property' => 'foo']); + self::assertInstanceOf(DummyWithProperty::class, $entity); + } + public function testCanPersistEntitiesWithoutExplicitIdentifierSetEvenWhenExistingEntitiesHaveOne(): void { $dummy1 = new Dummy();