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(); ... }
The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
The EntityManager is the central access point to ORM functionality. It can be used to find, persist, flush and remove entities.
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.
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.
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