15

At the moment I am learning how to use Symfony2. I got to the point where they explain how to use Doctrine.

In the examples given they sometimes use the entity manager:

$em = $this->getDoctrine()->getEntityManager();
$products = $em->getRepository('AcmeStoreBundle:Product')
        ->findAllOrderedByName();

and in other examples the entity manager is not used:

$product = $this->getDoctrine()
        ->getRepository('AcmeStoreBundle:Product')
        ->find($id);

So I actually tried the first example without getting the entity manager:

$repository = $this->getDoctrine()
        ->getRepository('AcmeStoreBundle:Product');
$products = $repository->findAllOrderedByName();

and got the same results.

So when do i actually need the entity manager and when is it OK to just go for the repository at once?

Mats Rietdijk
  • 2,576
  • 3
  • 20
  • 25

3 Answers3

29

Looking at Controller getDoctrine() equals to $this->get('doctrine'), an instance of Symfony\Bundle\DoctrineBundle\Registry. Registry provides:

Thus, $this->getDoctrine()->getRepository() equals $this->getDoctrine()->getEntityManager()->getRepository().

Entity manager is useful when you want to persist or remove an entity:

$em = $this->getDoctrine()->getEntityManager();

$em->persist($myEntity);
$em->flush();

If you are just fetching data, you can get only the repository:

$repository = $this->getDoctrine()->getRepository('AcmeStoreBundle:Product');
$product    = $repository->find(1);

Or better, if you are using custom repositories, wrap getRepository() in a controller function as you can get auto-completition feature from your IDE:

/**
 * @return \Acme\HelloBundle\Repository\ProductRepository
 */
protected function getProductRepository()
{
    return $this->getDoctrine()->getRepository('AcmeHelloBundle:Product');
}
gremo
  • 47,186
  • 75
  • 257
  • 421
  • I already knew I needed to use the entity manager when using `flush()`. Also the idea to use a `getProductRepository()` function is something that might be usefull, thanks! – Mats Rietdijk Aug 07 '12 at 14:13
  • 1
    @MatsRietdijk glad to be of help! I always wrap `$this->get('some service')` in custom functions in my `BaseController` to get auto completition... – gremo Aug 07 '12 at 14:35
  • 1
    What is the Location of `AcmeStoreBundle:Product ` where can i find `Product` in my Symfony Application. – Snopzer Jul 11 '16 at 11:56
  • 1
    `YouApplication\src\Bundle\ToolsBundle\Repository\ProductRepository.php` – Mohammad Fareed Jul 11 '16 at 12:02
2

I think that the getDoctrine()->getRepository() is simply a shortcut to getDoctrine()->getEntityManager()->getRepository(). Did not check the source code, but sounds rather reasonable to me.

Alessandro Santini
  • 1,943
  • 13
  • 17
0

If you plan to do multiple operations with the entity manager (like get a repository, persist an entity, flush, etc), then get the entity manager first and store it in a variable. Otherwise, you can get the repository from the entity manager and call whatever method you want on the repository class all in one line. Both ways will work. It's just a matter of coding style and your needs.

Leo Bedrosian
  • 3,659
  • 2
  • 18
  • 22