Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get entity manager for Doctrine entity with Symfony 2.1 from inside controller

How can I get an entity manager from inside a controller with latest Symfony and Doctrine?

The way described in "The Book" flagged as deprecated now. What is a modern (proper) way to do this?

public function someAction() {     // getEntityManager() from Doctrine\Bundle\DoctrineBundle\Registry is deprecated     $entityManager = $this->getDoctrine()->getEntityManager();     ... } 
like image 516
Slava Fomin II Avatar asked Oct 06 '12 21:10

Slava Fomin II


People also ask

What is EntityManager in Symfony?

The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.

What is Entity Manager in doctrine?

The EntityManager is the central access point to ORM functionality. It can be used to find, persist, flush and remove entities.

Does Symfony use doctrine?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.


1 Answers

Use $this->getDoctrine()->getManager() instead.

Actually, it's best not to make controllers aware of the persistence layer you're using. That stuff should be moved to the Service Layer to abstract the way the data is persisted.

like image 194
Elnur Abdurrakhimov Avatar answered Sep 18 '22 13:09

Elnur Abdurrakhimov