title: Symfony - How to access entity's repository
body: <p>There are several ways that we can access the entity's repository in Symfony2 controllers or services which each has its advantage and disadvantage. First I list them here and then asking if there is any better solution or these are the only options that we have and we should choose one or some based on our preferences. I also want to know if method 5 (which I've started to use it recently) can be good and doesn't break any rule or having any side effects.</p>
  
  <p><strong>Basic Method:</strong> Use entity manager in the controller or Inject it to a service and then accessing any repository that I want. This is the basic way of accessing a Repository in controller or service.</p>
  
  <pre><code>class DummyController
  {
      public function dummyAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;getRepository('ProductBundle:Product')-&gt;loadProduct($id);
      }
  }
  </code></pre>
  
  <p>But there are some problems related to this method. The first problem is that I cannot do Ctrl + click on for example loadProduct function and go directly to its implementation (Unless there is a way that I don't know). The other problem is that I will end up repeating this part of code over and over.</p>
  
  <p><strong>Method 2:</strong> The other method is just to define a getter in my service or controller to access my repository.</p>
  
  <pre><code>class DummyService
  {
      protected $em;
  
      public function __construct(EntityManager $em)
      {
          $this-&gt;em = $em;
      } 
  
      public function dummyFunction($id)
      {
          $this-&gt;getProductRepository()-&gt;loadProduct($id);
      }
  
      /**
       * @return \ProductBundle\Entity\Repository\ProductRepository
       */
      public function getProductRepository()
      {
          return $this-&gt;em-&gt;getRepository('ProductBundle:Product');
      }
  }
  </code></pre>
  
  <p>This method solves the first problem and somehow the second but still, I have
  to repeat all getters that I need in my service or controller, also I will have several getters in my services and controllers just for accessing to repositories</p>
  
  <p><strong>Method 3:</strong> Another way is to inject a repository to my service, it is nice especially if we have a good control on our code and we are not involved with other developers who inject the entire Container into your service.</p>
  
  <pre><code>class DummyService
  {
      protected $productRepository;
  
      public function __construct(ProductRepository $productRepository)
      {
          $this-&gt;productRepository = $productRepository;
      } 
  
      public function dummyFunction($id)
      {
          $this-&gt;productRepository-&gt;loadProduct($id);
      }
  }
  </code></pre>
  
  <p>This method solves the first and second problem, but if my service is big
  and it needs to deal with a lot of repositories then it is not a nice idea to
  inject for example 10 repository to my service.</p>
  
  <p><strong>Method 4:</strong> Another way is to have a service to wrap all of my repositories and inject this service to other services.</p>
  
  <pre><code>class DummyService
  {
      protected $repositoryService;
  
      public function __construct(RepositoryService $repositoryService)
      {
          $this-&gt;repositoryService = $repositoryService;
      } 
  
      public function dummyFunction($id)
      {
          $this-&gt;repositoryService-&gt;getProductRepository()-&gt;loadProduct($id);
      }
  }
  </code></pre>
  
  <p>RepositoryService:</p>
  
  <pre><code>class RepositoryService
  {
      protected $em;
  
      public function __construct(EntityManager $em)
      {
          $this-&gt;em = $em;
      } 
  
      /**
       * @return \ProductBundle\Entity\Repository\ProductRepository
       */
      public function getProductRepository()
      {
          return $this-&gt;em-&gt;getRepository('ProductBundle:Product');
      }
  
      /**
       * @return \CmsBundle\Entity\Repository\PageRepository
       */
      public function getPageRepository()
      {
          return $this-&gt;em-&gt;getRepository('CmsBundle:Page');
      }
  }
  </code></pre>
  
  <p>This method also solves the first and second problem. But RepositoryService can become so big when we have for example 200 entities.</p>
  
  <p><strong>Method 5:</strong> Finally I can define a static method in each entity which returns its repository.</p>
  
  <pre><code>class DummyService
  {
      protected $em;
  
      public function __construct(EntityManager $em)
      {
          $this-&gt;em = $em;
      } 
  
      public function dummyFunction($id)
      {
          Product::getRepository($this-&gt;em)-&gt;loadProduct($id);
      }
  }
  </code></pre>
  
  <p>My Entity:</p>
  
  <pre><code>/**
   * Product
   *
   * @ORM\Table(name="saman_product")
   * @ORM\Entity(repositoryClass="ProductBundle\Entity\ProductRepository")
   */
  class Product
  {
      /**
       *
       * @param \Doctrine\ORM\EntityManagerInterface $em
       * @return \ProductBundle\Entity\ProductRepository
       */
      public static function getRepository(EntityManagerInterface $em)
      {
          return $em-&gt;getRepository(__CLASS__);
      }   
  }
  </code></pre>
  
  <p>This method solves the first and second problem also I do not need to define a
  service to access repositories. I've used it recently and so far its the best method for me. I don’t think this method will break the rule of entities since it's defined in the class scope and also is so thine. But still I am not sure about it and whether or not it has any side effects.</p>