Переглянути джерело

Merge pull request #36 from gimler/multiple_em

use doctrine registry to find the correct entity manager for class
Thomas 13 роки тому
батько
коміт
7b78871916
2 змінених файлів з 40 додано та 29 видалено
  1. 38 27
      Model/ModelManager.php
  2. 2 2
      Resources/config/doctrine_orm.xml

+ 38 - 27
Model/ModelManager.php

@@ -12,6 +12,7 @@
 namespace Sonata\DoctrineORMAdminBundle\Model;
 
 use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription;
+use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
 
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
@@ -19,23 +20,23 @@ use Sonata\AdminBundle\Datagrid\DatagridInterface;
 use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 use Sonata\AdminBundle\Exception\ModelManagerException;
 
-use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\QueryBuilder;
 
 use Symfony\Component\Form\Exception\PropertyAccessDeniedException;
-use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
+
+use Symfony\Bridge\Doctrine\RegistryInterface;
 
 class ModelManager implements ModelManagerInterface
 {
-    protected $entityManager;
+    protected $registry;
 
     /**
      *
      * @param \Doctrine\ORM\EntityManager $entityManager
      */
-    public function __construct(EntityManager $entityManager)
+    public function __construct(RegistryInterface $registry)
     {
-        $this->entityManager = $entityManager;
+        $this->registry = $registry;
     }
 
     /**
@@ -47,7 +48,7 @@ class ModelManager implements ModelManagerInterface
      */
     public function getMetadata($class)
     {
-        return $this->entityManager->getMetadataFactory()->getMetadataFor($class);
+        return $this->getEntityManager($class)->getMetadataFactory()->getMetadataFor($class);
     }
 
     /**
@@ -58,7 +59,7 @@ class ModelManager implements ModelManagerInterface
      */
     public function hasMetadata($class)
     {
-        return $this->entityManager->getMetadataFactory()->hasMetadataFor($class);
+        return $this->getEntityManager($class)->getMetadataFactory()->hasMetadataFor($class);
     }
 
     /**
@@ -96,8 +97,9 @@ class ModelManager implements ModelManagerInterface
     public function create($object)
     {
         try {
-            $this->entityManager->persist($object);
-            $this->entityManager->flush();
+            $entityManager = $this->getEntityManager($object);
+            $entityManager->persist($object);
+            $entityManager->flush();
         } catch ( \PDOException $e ) {
             throw new ModelManagerException('', 0, $e);
         }
@@ -106,8 +108,9 @@ class ModelManager implements ModelManagerInterface
     public function update($object)
     {
         try {
-            $this->entityManager->persist($object);
-            $this->entityManager->flush();
+            $entityManager = $this->getEntityManager($object);
+            $entityManager->persist($object);
+            $entityManager->flush();
         } catch ( \PDOException $e ) {
             throw new ModelManagerException('', 0, $e);
         }
@@ -116,8 +119,9 @@ class ModelManager implements ModelManagerInterface
     public function delete($object)
     {
         try {
-            $this->entityManager->remove($object);
-            $this->entityManager->flush();
+            $entityManager = $this->getEntityManager($object);
+            $entityManager->remove($object);
+            $entityManager->flush();
         } catch ( \PDOException $e ) {
             throw new ModelManagerException('', 0, $e);
         }
@@ -133,7 +137,7 @@ class ModelManager implements ModelManagerInterface
     public function find($class, $id)
     {
         $values = array_combine($this->getIdentifierFieldNames($class), explode('-', $id));
-        return $this->entityManager->getRepository($class)->find($values);
+        return $this->getEntityManager($class)->getRepository($class)->find($values);
     }
 
     /**
@@ -143,7 +147,7 @@ class ModelManager implements ModelManagerInterface
      */
     public function findBy($class, array $criteria = array())
     {
-        return $this->entityManager->getRepository($class)->findBy($criteria);
+        return $this->getEntityManager($class)->getRepository($class)->findBy($criteria);
     }
 
     /**
@@ -153,15 +157,19 @@ class ModelManager implements ModelManagerInterface
      */
     public function findOneBy($class, array $criteria = array())
     {
-        return $this->entityManager->getRepository($class)->findOneBy($criteria);
+        return $this->getEntityManager($class)->getRepository($class)->findOneBy($criteria);
     }
 
     /**
      * @return \Doctrine\ORM\EntityManager
      */
-    public function getEntityManager()
+    public function getEntityManager($class)
     {
-        return $this->entityManager;
+        if (is_object($class)) {
+            $class = get_class($class);
+        }
+
+        return $this->registry->getEntityManagerForClass($class);
     }
 
     /**
@@ -191,7 +199,7 @@ class ModelManager implements ModelManagerInterface
      */
     public function createQuery($class, $alias = 'o')
     {
-        $repository = $this->getEntityManager()->getRepository($class);
+        $repository = $this->getEntityManager($class)->getRepository($class);
 
         return new ProxyQuery($repository->createQueryBuilder($alias));
     }
@@ -225,11 +233,12 @@ class ModelManager implements ModelManagerInterface
      */
     public function getIdentifierValues($entity)
     {
-        if (!$this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
+        $entityManager = $this->getEntityManager($entity);
+        if (!$entityManager->getUnitOfWork()->isInIdentityMap($entity)) {
             throw new \RuntimeException('Entities passed to the choice field must be managed');
         }
 
-        return $this->getEntityManager()->getUnitOfWork()->getEntityIdentifier($entity);
+        return $entityManager->getUnitOfWork()->getEntityIdentifier($entity);
     }
 
     /**
@@ -253,7 +262,7 @@ class ModelManager implements ModelManagerInterface
         }
 
         // the entities is not managed
-        if (!$entity || !$this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
+        if (!$entity || !$this->getEntityManager($entity)->getUnitOfWork()->isInIdentityMap($entity)) {
             return null;
         }
 
@@ -301,18 +310,20 @@ class ModelManager implements ModelManagerInterface
     public function batchDelete($class, ProxyQueryInterface $queryProxy)
     {
         try {
+            $entityManager = $this->getEntityManager($class);
+
             $i = 0;
             foreach ($queryProxy->getQuery()->iterate() as $pos => $object) {
-                $this->entityManager->remove($object[0]);
+                $entityManager->remove($object[0]);
 
                 if ((++$i % 20) == 0) {
-                    $this->entityManager->flush();
-                    $this->entityManager->clear();
+                    $entityManager->flush();
+                    $entityManager->clear();
                 }
             }
 
-            $this->entityManager->flush();
-            $this->entityManager->clear();
+            $entityManager->flush();
+            $entityManager->clear();
         } catch ( \PDOException $e ) {
             throw new ModelManagerException('', 0, $e);
         }

+ 2 - 2
Resources/config/doctrine_orm.xml

@@ -10,7 +10,7 @@
         </service>
 
         <service id="sonata.admin.manager.orm" class="Sonata\DoctrineORMAdminBundle\Model\ModelManager">
-            <argument type="service" id="sonata.admin.entity_manager" />
+            <argument type="service" id="doctrine" />
         </service>
 
         <!-- FormBuilder -->
@@ -64,4 +64,4 @@
             <argument />
         </service>
     </services>
-</container>
+</container>