[DoctrineBundle, WebBundle] Enhancing API for retrieving database connections and entity managers from controllers to allow for specifying the name of the connection or entity manager. If no argument is given it returns the default configured connection or entity manager.

This commit is contained in:
Jonathan H. Wage 2010-02-25 21:04:13 -05:00 committed by Fabien Potencier
parent 2db073b03a
commit 6ead924835
2 changed files with 48 additions and 13 deletions

View File

@ -16,25 +16,65 @@ use Symfony\Components\RequestHandler\Exception\NotFoundHttpException;
*/
/**
*
* Doctrine ORM controller gives you access to entity managers and DQL queries.
*
* @package symfony
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class DoctrineController extends Controller
{
protected function getEntityManager()
public function getDatabaseConnection($name = null)
{
return $this->container->getDoctrine_ORM_EntityManagerService();
if ($name)
{
return $this->container->getService(sprintf('doctrine.dbal.%s_connection', $name));
}
else
{
return $this->container->getDatabaseConnectionService();
}
}
public function createQueryBuilder()
/**
* Get the default entity manager service or the entity manager
* with the given name.
*
* @param string $name Optional entity manager service name
* @return object $em
*/
protected function getEntityManager($name = null)
{
return $this->getEntityManager()->createQueryBuilder();
if ($name)
{
return $this->container->getService(sprintf('doctrine.orm.%s_entity_manager', $name));
}
else
{
return $this->container->getDoctrine_ORM_EntityManagerService();
}
}
public function createQuery($dql = '')
/**
* Create a new QueryBuilder instance.
*
* @param string $name Optional entity manager service name
* @return object QueryBuilder
*/
public function createQueryBuilder($name = null)
{
return $this->getEntityManager()->createQuery($dql);
return $this->getEntityManager($name)->createQueryBuilder();
}
/**
* Create a new Query instance.
*
* @param string $dql Optional Dql string to create the query from
* @param string $name Optional entity manager service name
* @return object QueryBuilder
*/
public function createQuery($dql = '', $name = null)
{
return $this->getEntityManager($name)->createQuery($dql);
}
}

View File

@ -14,7 +14,7 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
*/
/**
*
* WebBundle Controller gives you convenient access to all commonly needed services.
*
* @package symfony
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
@ -38,11 +38,6 @@ class Controller
return $this->container->getUserService();
}
public function getDatabaseConnection()
{
return $this->container->getDatabaseConnectionService();
}
public function getMailer()
{
return $this->container->getMailerService();