فهرست منبع

[FrameworkBundle][DoctrineBundle] Adding a few shortcut methods

This adds to convience methods, for two separate reasons:

* Controller::getDoctrine() - this will allow method completion on the Registry class to work in IDEs, is slightly shorter, and should feel very "concrete" to beginners

* Registry::getRepository() - the repository is a very convenient thing to need - this allows it to be fetched much more succintly

Overall Before:

    $product = $this->get('doctrine')
        ->getEntityManager()
        ->getRepository('AcmeDemoBundle:Product')
        ->find($id);

Overall After (with IDE method auto-completion for `getRepository`):

    $product = $this->getDoctrine()
        ->getRepository('AcmeDemoBundle:Product')
        ->find($id);
Ryan Weaver 14 سال پیش
والد
کامیت
28dcb3c581
2فایلهای تغییر یافته به همراه23 افزوده شده و 0 حذف شده
  1. 13 0
      src/Symfony/Bundle/DoctrineBundle/Registry.php
  2. 10 0
      src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

+ 13 - 0
src/Symfony/Bundle/DoctrineBundle/Registry.php

@@ -108,6 +108,19 @@ class Registry
         return $this->container->get($this->entityManagers[$name]);
     }
 
+    /**
+     * Shortcut method to return the EntityRepository for an entity.
+     *
+     * @param string $entityName The name of the entity.
+     * @param string $entityManagerNAme The entity manager name (null for the default one)
+     * @return Doctrine\ORM\EntityRepository
+     */
+    public function getRepository($entityName, $entityManagerName)
+    {
+        return $this->getEntityManager($entityManagerName)
+            ->getRespository($entityName);
+    }
+
     /**
      * Resets a named entity manager.
      *

+ 10 - 0
src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

@@ -137,6 +137,16 @@ class Controller extends ContainerAware
         return $this->get('form.factory')->createBuilder('form', $data, $options);
     }
 
+    /**
+     * Shortcut to return the Doctrine Registry class
+     *
+     * @return Symfony\Bundle\DoctrineBundle\Registry
+     */
+    public function getDoctrine()
+    {
+        return $this->get('doctrine');
+    }
+
     /**
      * Returns true if the service id is defined.
      *