Skip to content

Commit

Permalink
fix: clear entity persister on restoring class metadata (#264)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wickedOne authored Mar 18, 2024
1 parent 4958eaf commit c05882a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
30 changes: 30 additions & 0 deletions fixtures/Bridge/Doctrine/Entity/DummyWithProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Entity;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;

/**
* @Entity
*/
class DummyWithProperty
{
/**
* @Id
*
* @Column(type="integer")
*
* @GeneratedValue
*/
public int $id;

/**
* @Column(type="string", name="property", nullable=true)
*/
public ?string $property = null;
}
1 change: 1 addition & 0 deletions src/Bridge/Doctrine/Persister/ObjectManagerPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function flush(): void

foreach ($this->metadataToRestore as $metadata) {
$metadataFactory->setMetadataFor($metadata->getName(), $metadata);
$this->clearUnitOfWorkPersister($metadata->getName());
}

$this->metadataToRestore = [];
Expand Down
19 changes: 19 additions & 0 deletions tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c05882a

Please sign in to comment.