ソースを参照

[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.

Jonathan H. Wage 15 年 前
コミット
6ead924835

+ 47 - 7
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 <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);
   }
 }

+ 1 - 6
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 <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();