diff --git a/src/Bridge/Doctrine/Persister/ObjectManagerPersister.php b/src/Bridge/Doctrine/Persister/ObjectManagerPersister.php index f8946c89..87247527 100644 --- a/src/Bridge/Doctrine/Persister/ObjectManagerPersister.php +++ b/src/Bridge/Doctrine/Persister/ObjectManagerPersister.php @@ -13,6 +13,7 @@ namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Persister; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Common\Persistence\Mapping\ClassMetadata; use Doctrine\Common\Persistence\ObjectManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo as ODMClassMetadataInfo; @@ -27,21 +28,17 @@ class ObjectManagerPersister implements PersisterInterface { use IsAServiceTrait; - private $objectManager; - - /** - * @var array|null Values are FQCN of persistable objects - */ - private $persistableClasses; + /** @var ManagerRegistry $managerRegistry */ + private $managerRegistry; /** * @var ClassMetadata[] Entity metadata, FQCN being the key */ private $metadata = []; - public function __construct(ObjectManager $manager) + public function __construct(ManagerRegistry $managerRegistry) { - $this->objectManager = $manager; + $this->managerRegistry = $managerRegistry; } /** @@ -49,14 +46,12 @@ public function __construct(ObjectManager $manager) */ public function persist($object) { - if (null === $this->persistableClasses) { - $this->persistableClasses = array_flip($this->getPersistableClasses($this->objectManager)); - } - $class = get_class($object); - if (isset($this->persistableClasses[$class])) { - $metadata = $this->getMetadata($class); + $manager = $this->managerRegistry->getManagerForClass($class); + + if ($manager) { + $metadata = $this->getClassMetadata($manager, $class); $generator = null; $generatorType = null; @@ -78,7 +73,7 @@ public function persist($object) } try { - $this->objectManager->persist($object); + $manager->persist($object); } catch (ORMException $exception) { if ($metadata->idGenerator instanceof ORMAssignedGenerator) { throw ObjectGeneratorPersisterExceptionFactory::createForEntityMissingAssignedIdForField($object); @@ -100,27 +95,9 @@ public function persist($object) */ public function flush() { - $this->objectManager->flush(); - } - - /** - * @return string[] - */ - private function getPersistableClasses(ObjectManager $manager): array - { - $persistableClasses = []; - $allMetadata = $manager->getMetadataFactory()->getAllMetadata(); - - foreach ($allMetadata as $metadata) { - /** @var ORMClassMetadataInfo|ODMClassMetadataInfo $metadata */ - if (false === $metadata->isMappedSuperclass - && false === (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass) - ) { - $persistableClasses[] = $metadata->getName(); - } + foreach ($this->managerRegistry->getManagers() as $manager) { + $manager->flush(); } - - return $persistableClasses; } protected function configureIdGenerator(ORMClassMetadataInfo $metadata): void @@ -129,11 +106,10 @@ protected function configureIdGenerator(ORMClassMetadataInfo $metadata): void $metadata->setIdGenerator(new ORMAssignedGenerator()); } - private function getMetadata(string $class): ClassMetadata + private function getClassMetadata(ObjectManager $manager, string $class): ClassMetadata { if (false === array_key_exists($class, $this->metadata)) { - $classMetadata = $this->objectManager->getClassMetadata($class); - $this->metadata[$class] = $classMetadata; + $this->metadata[$class] = $manager->getClassMetadata($class); } return $this->metadata[$class]; diff --git a/src/Bridge/Symfony/Resources/config/doctrine_mongodb_odm.xml b/src/Bridge/Symfony/Resources/config/doctrine_mongodb_odm.xml index 4068f490..98a52b88 100644 --- a/src/Bridge/Symfony/Resources/config/doctrine_mongodb_odm.xml +++ b/src/Bridge/Symfony/Resources/config/doctrine_mongodb_odm.xml @@ -52,6 +52,7 @@ + @@ -71,7 +72,7 @@ - + diff --git a/src/Bridge/Symfony/Resources/config/doctrine_orm.xml b/src/Bridge/Symfony/Resources/config/doctrine_orm.xml index b2906397..2289260a 100644 --- a/src/Bridge/Symfony/Resources/config/doctrine_orm.xml +++ b/src/Bridge/Symfony/Resources/config/doctrine_orm.xml @@ -52,6 +52,7 @@ + @@ -79,7 +80,7 @@ - + diff --git a/src/Bridge/Symfony/Resources/config/doctrine_phpcr_odm.xml b/src/Bridge/Symfony/Resources/config/doctrine_phpcr_odm.xml index 3b94e524..bdec5296 100644 --- a/src/Bridge/Symfony/Resources/config/doctrine_phpcr_odm.xml +++ b/src/Bridge/Symfony/Resources/config/doctrine_phpcr_odm.xml @@ -52,12 +52,14 @@ + + The service "%service_id%s" is deprecated and will be removed in future versions. Use "fidry_alice_data_fixtures.persistence.doctrine_phpcr.purger.purger_factory" instead. @@ -73,7 +75,7 @@ - + diff --git a/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php b/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php index 5c0eef5c..8d0ebf41 100644 --- a/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php +++ b/tests/Bridge/Doctrine/Persister/ObjectManagerPersisterTest.php @@ -14,6 +14,7 @@ namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Persister; use Doctrine\Common\DataFixtures\Purger\ORMPurger; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMException; use Doctrine\ORM\ORMInvalidArgumentException; @@ -25,6 +26,7 @@ use Fidry\AliceDataFixtures\Bridge\Doctrine\Entity\MappedSuperclassDummy; use Fidry\AliceDataFixtures\Exception\ObjectGeneratorPersisterException; use Fidry\AliceDataFixtures\Persistence\PersisterInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -48,13 +50,23 @@ class ObjectManagerPersisterTest extends TestCase */ private $purger; + /** + * @var ManagerRegistry|MockObject + */ + private $managerRegistry; + /** * @inheritdoc */ public function setUp() { $this->entityManager = $GLOBALS['entity_manager']; - $this->persister = new ObjectManagerPersister($this->entityManager); + + $this->managerRegistry = $this->createMock(ManagerRegistry::class); + $this->managerRegistry->method('getManagerForClass')->willReturn($this->entityManager); + $this->managerRegistry->method('getManagers')->willReturn([$this->entityManager]); + + $this->persister = new ObjectManagerPersister($this->managerRegistry); $this->purger = new ORMPurger($this->entityManager); }