浏览代码

added Registry::getEntityManagerForObject() to conveniently get the entity manager associated with a given Entity

Fabien Potencier 14 年之前
父节点
当前提交
090a51a0fe
共有 2 个文件被更改,包括 27 次插入0 次删除
  1. 9 0
      src/Symfony/Bridge/Doctrine/RegistryInterface.php
  2. 18 0
      src/Symfony/Bundle/DoctrineBundle/Registry.php

+ 9 - 0
src/Symfony/Bridge/Doctrine/RegistryInterface.php

@@ -123,4 +123,13 @@ interface RegistryInterface
      * @return Doctrine\ORM\EntityRepository
      */
     function getRepository($entityName, $entityManagerName = null);
+
+    /**
+     * Gets the entity manager associated with a given object.
+     *
+     * @param object $object A Doctrine Entity
+     *
+     * @return EntityManager|null
+     */
+    function getEntityManagerForObject($object);
 }

+ 18 - 0
src/Symfony/Bundle/DoctrineBundle/Registry.php

@@ -216,4 +216,22 @@ class Registry implements RegistryInterface
     {
         return $this->getEntityManager($entityManagerName)->getRepository($entityName);
     }
+
+    /**
+     * Gets the entity manager associated with a given object.
+     *
+     * @param object $object A Doctrine Entity
+     *
+     * @return EntityManager|null
+     */
+    public function getEntityManagerForObject($object)
+    {
+        foreach ($this->entityManagers as $id) {
+            $em = $this->container->get($id);
+
+            if ($em->getConfiguration()->getMetadataDriverImpl()->isTransient($object)) {
+                return $em;
+            }
+        }
+    }
 }