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?
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.
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.
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.
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.
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');
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With