Skip to content

Commit

Permalink
added Registry::getEntityManagerForObject() to conveniently get the e…
Browse files Browse the repository at this point in the history
…ntity manager associated with a given Entity
  • Loading branch information
fabpot committed Jul 6, 2011
1 parent d3b7807 commit 090a51a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Symfony/Bridge/Doctrine/RegistryInterface.php
Expand Up @@ -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);
}
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/DoctrineBundle/Registry.php
Expand Up @@ -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;
}
}
}
}

0 comments on commit 090a51a

Please sign in to comment.