From 6ead9248354faae9509d4e021c6ca17d0d52a392 Mon Sep 17 00:00:00 2001 From: "Jonathan H. Wage" Date: Thu, 25 Feb 2010 21:04:13 -0500 Subject: [PATCH] [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. --- .../Controller/DoctrineController.php | 54 ++++++++++++++++--- .../Framework/WebBundle/Controller.php | 7 +-- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Framework/DoctrineBundle/Controller/DoctrineController.php b/src/Symfony/Framework/DoctrineBundle/Controller/DoctrineController.php index dfb757bfb7..ada36da00e 100644 --- a/src/Symfony/Framework/DoctrineBundle/Controller/DoctrineController.php +++ b/src/Symfony/Framework/DoctrineBundle/Controller/DoctrineController.php @@ -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 + * @author Jonathan H. Wage */ 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); } } diff --git a/src/Symfony/Framework/WebBundle/Controller.php b/src/Symfony/Framework/WebBundle/Controller.php index 117759f513..8803e4f8b5 100644 --- a/src/Symfony/Framework/WebBundle/Controller.php +++ b/src/Symfony/Framework/WebBundle/Controller.php @@ -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 @@ -38,11 +38,6 @@ class Controller return $this->container->getUserService(); } - public function getDatabaseConnection() - { - return $this->container->getDatabaseConnectionService(); - } - public function getMailer() { return $this->container->getMailerService();