From 090a51a0fe076b0667018d333cb307cd73cdfe5d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 6 Jul 2011 12:33:49 +0200 Subject: [PATCH] added Registry::getEntityManagerForObject() to conveniently get the entity manager associated with a given Entity --- .../Bridge/Doctrine/RegistryInterface.php | 9 +++++++++ src/Symfony/Bundle/DoctrineBundle/Registry.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Symfony/Bridge/Doctrine/RegistryInterface.php b/src/Symfony/Bridge/Doctrine/RegistryInterface.php index 08862418b777..73f67a2ad815 100644 --- a/src/Symfony/Bridge/Doctrine/RegistryInterface.php +++ b/src/Symfony/Bridge/Doctrine/RegistryInterface.php @@ -123,4 +123,13 @@ function getEntityManagerNames(); * @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); } diff --git a/src/Symfony/Bundle/DoctrineBundle/Registry.php b/src/Symfony/Bundle/DoctrineBundle/Registry.php index a435119c87b3..d50a0b8510aa 100644 --- a/src/Symfony/Bundle/DoctrineBundle/Registry.php +++ b/src/Symfony/Bundle/DoctrineBundle/Registry.php @@ -216,4 +216,22 @@ public function getRepository($entityName, $entityManagerName = null) { 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; + } + } + } }