From d1cfa080443fae9c66bdf4c4e375a38fbcfe7b6b Mon Sep 17 00:00:00 2001 From: gmorel Date: Tue, 12 Jan 2016 16:06:12 +0100 Subject: [PATCH] Now work with several entity managers - persist operation search for the good manager in registry - purge will purge each managers registered. --- .../Adapter/ORM/ORMSubscriberFactory.php | 1 - .../AliceExtension/Doctrine/ORMPersister.php | 70 +++++++++++++++++++ .../AliceExtension/Doctrine/ORMPurger.php | 53 +++++--------- .../AliceExtension/Resources/services.xml | 10 +-- 4 files changed, 91 insertions(+), 43 deletions(-) create mode 100644 src/Rezzza/AliceExtension/Doctrine/ORMPersister.php diff --git a/src/Rezzza/AliceExtension/Adapter/ORM/ORMSubscriberFactory.php b/src/Rezzza/AliceExtension/Adapter/ORM/ORMSubscriberFactory.php index 584e2b7..605677c 100644 --- a/src/Rezzza/AliceExtension/Adapter/ORM/ORMSubscriberFactory.php +++ b/src/Rezzza/AliceExtension/Adapter/ORM/ORMSubscriberFactory.php @@ -3,7 +3,6 @@ namespace Rezzza\AliceExtension\Adapter\ORM; use Doctrine\Common\Persistence\ManagerRegistry; -use Doctrine\Fixture\Persistence\ManagerRegistryEventSubscriber; use Nelmio\Alice\ORM\Doctrine as ORMPersister; use Rezzza\AliceExtension\Doctrine\ORMPurger; diff --git a/src/Rezzza/AliceExtension/Doctrine/ORMPersister.php b/src/Rezzza/AliceExtension/Doctrine/ORMPersister.php new file mode 100644 index 0000000..eab1edc --- /dev/null +++ b/src/Rezzza/AliceExtension/Doctrine/ORMPersister.php @@ -0,0 +1,70 @@ +registry = $registry; + $this->flush = $doFlush; + } + + /** + * {@inheritDoc} + */ + public function persist(array $objects) + { + $managersToFlush = []; + + foreach ($objects as $object) { + $managerName = $this->findManagerForClass(get_class($object)); + $this->registry->getManager($managerName)->persist($object); + + $managersToFlush[$managerName] = $managerName; + } + + if ($this->flush) { + foreach ($managersToFlush as $managerName) { + $this->registry->getManager($managerName)->flush(); + } + } + } + + /** + * {@inheritDoc} + */ + public function find($class, $id) + { + $managerName = $this->findManagerForClass($class); + $entity = $this->registry->getManager($managerName)->find($class, $id); + + if (!$entity) { + throw new \UnexpectedValueException('Entity with Id ' . $id . ' and Class ' . $class . ' not found'); + } + + return $entity; + } + + /** + * @param string $entityClass + * @return string + * @throws \Doctrine\Common\Persistence\Mapping\MappingException + */ + private function findManagerForClass($entityClass) + { + foreach ($this->registry->getManagers() as $name => $manager) { + if ($manager->getMetadataFactory()->hasMetadataFor($entityClass)) { + return $name; + } + } + + throw new \Doctrine\Common\Persistence\Mapping\MappingException(sprintf('Entity class "%s" not found in managers.', $entityClass)); + } +} diff --git a/src/Rezzza/AliceExtension/Doctrine/ORMPurger.php b/src/Rezzza/AliceExtension/Doctrine/ORMPurger.php index 79400c7..e6b08dc 100644 --- a/src/Rezzza/AliceExtension/Doctrine/ORMPurger.php +++ b/src/Rezzza/AliceExtension/Doctrine/ORMPurger.php @@ -19,10 +19,10 @@ namespace Rezzza\AliceExtension\Doctrine; +use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\ORM\EntityManager; -use Doctrine\ORM\Internal\CommitOrderCalculator; use Doctrine\ORM\Mapping\ClassMetadata; -use Doctrine\DBAL\Platforms\MySqlPlatform; /** * Class responsible for purging databases of data before reloading data fixtures. @@ -35,8 +35,8 @@ class ORMPurger const PURGE_MODE_DELETE = 1; const PURGE_MODE_TRUNCATE = 2; - /** EntityManager instance used for persistence. */ - private $em; + /** ManagerRegistry instance used for persistence. */ + private $registry; /** * If the purge should be done through DELETE or TRUNCATE statements @@ -50,9 +50,9 @@ class ORMPurger * * @param EntityManager $em EntityManager instance used for persistence. */ - public function __construct(EntityManager $em = null) + public function __construct(ManagerRegistry $registry) { - $this->em = $em; + $this->registry = $registry; } /** @@ -76,38 +76,21 @@ public function getPurgeMode() return $this->purgeMode; } - /** - * Set the EntityManager instance this purger instance should use. - * - * @param EntityManager $em - */ - public function setEntityManager(EntityManager $em) - { - $this->em = $em; - } - - /** - * Retrieve the EntityManager instance this purger instance is using. - * - * @return \Doctrine\ORM\EntityManager - */ - public function getObjectManager() + /** @inheritDoc */ + public function purge() { - return $this->em; + foreach ($this->registry->getManagers() as $manager) { + $this->purgeManager($manager); + } } - /** @inheritDoc */ - public function purge() + private function purgeManager(EntityManager $entityManager) { - $metadatas = $this->em->getMetadataFactory()->getAllMetadata(); - $platform = $this->em->getConnection()->getDatabasePlatform(); + $metadatas = $entityManager->getMetadataFactory()->getAllMetadata(); + $platform = $entityManager->getConnection()->getDatabasePlatform(); $tables = array(); foreach ($metadatas as $metadata) { - if (true === $metadata->isEmbeddedClass) { - continue; - } - if (!$metadata->isMappedSuperclass) { $tables[] = $metadata->getQuotedTableName($platform); } @@ -121,20 +104,20 @@ public function purge() // implements hack for Mysql if ($platform instanceof MySqlPlatform) { - $this->em->getConnection()->exec('SET foreign_key_checks = 0;'); + $entityManager->getConnection()->exec('SET foreign_key_checks = 0;'); } foreach ($tables as $tbl) { if ($this->purgeMode === self::PURGE_MODE_DELETE) { - $this->em->getConnection()->executeUpdate("DELETE IGNORE FROM " . $tbl); + $entityManager->getConnection()->executeUpdate("DELETE IGNORE FROM " . $tbl); } else { - $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true)); + $entityManager->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true)); } } // implements hack for Mysql if ($platform instanceof MySqlPlatform) { - $this->em->getConnection()->exec('SET foreign_key_checks = 1;'); + $entityManager->getConnection()->exec('SET foreign_key_checks = 1;'); } } } diff --git a/src/Rezzza/AliceExtension/Resources/services.xml b/src/Rezzza/AliceExtension/Resources/services.xml index 99a89aa..8e2c196 100644 --- a/src/Rezzza/AliceExtension/Resources/services.xml +++ b/src/Rezzza/AliceExtension/Resources/services.xml @@ -23,7 +23,7 @@ Rezzza\AliceExtension\Adapter\SubscriberFactoryRegistry Rezzza\AliceExtension\Adapter\Elastica\ElasticaSubscriberFactory Rezzza\AliceExtension\Adapter\Elastica\Persister - Nelmio\Alice\ORM\Doctrine + Rezzza\AliceExtension\Doctrine\ORMPersister Rezzza\AliceExtension\Doctrine\ORMPurger @@ -61,10 +61,6 @@ alice_extension.processor.registry - - doctrine.orm.default_entity_manager - - doctrine @@ -110,11 +106,11 @@ - + - +