Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Entity Manager in Symfony2

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?

like image 770
Mats Rietdijk Avatar asked Aug 07 '12 13:08

Mats Rietdijk


People also ask

Why entity manager is closed?

The EntityManager becomes closed as soon as an SQL exception is thrown by the underlying connection. The "real" exception has surely occurred before that. You will need a new EntityManager.

What is a entity in Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.

What is persist flush?

Persist and Flush​ flush() . em. persist(entity) is used to mark new entities for future persisting. It will make the entity managed by given EntityManager and once flush will be called, it will be written to the database.


2 Answers

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

  • getEntityManager() returning Doctrine\ORM\EntityManager, which in turn provides getRepository()
  • getRepository() returning Doctrine\ORM\EntityRepository

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');
}
like image 159
gremo Avatar answered Sep 28 '22 01:09

gremo


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.

like image 21
Alessandro Santini Avatar answered Sep 28 '22 02:09

Alessandro Santini