Symfony2.3 Better way to get EntityManager inside a Controller

11,869

If you must have the EntityManager available in your constructor, a good way to get it is injecting it to the constructor.

To do this you must define your controller as a service.

# src/Acme/DemoBundle/Resources/config/services.yml
parameters:
    # ...
    acme.controller.quazbar.class: Acme\DemoBundle\Controller\QuazBarController

services:
    acme.quazbar.controller:
        class: "%acme.controller.quazbar.class%"
    # inject doctrine to the constructor as an argument
    arguments: [ @doctrine.orm.entity_manager ] 

Now all you have to do is modify your controller:

use Doctrine\ORM\EntityManager;

/**
 * QuazBar controller.
 *
 */
class QuazBarController extends Controller
{

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
    // ...
}

If you do not require the Entity Manager in the constructor, you can simply get it using the Dependency Injection Container from any method in your controller:

$this->getDoctrine()->getManager();

OR

$this->container->get('doctrine')->getManager();

Controller/setter injection is a good choice because you are not coupling your controller implementation to the DI Container.

At the end which one you use is up to your needs.

Share:
11,869
NBPalomino
Author by

NBPalomino

Peruvian Teacher, who love programming and learn new languages. El más alto cargo que un ciudadano puede ejercer en una Democracia; Es el de ser Maestro de Escuela - José A. Encinas.

Updated on June 13, 2022

Comments

  • NBPalomino
    NBPalomino almost 2 years

    I'm using Symfony2.3 and I currently using EntityManager as shown inside __construct()

    Which its a better aproach using EntityManager from __construct() or using inside each method ? as shown in public indexAction()

    /**
     * QuazBar controller.
     *
     */
    class QuazBarController extends Controller
    {
    
        public function __construct()
        {
            $this->em = $GLOBALS['kernel']->getContainer()->get('doctrine')->getManager();
        }
    
        /**
         * Lists all QuazBar entities.
         *
         */
        public function indexAction(Request $request)
        {
            $session    = $request->getSession();
            $pagina     = $request->query->get('page', 1);
            $em         = $this->getDoctrine()->getManager();
        }
    
  • NBPalomino
    NBPalomino about 10 years
    Thanks, but if I have a many Controllers (50~) is not a good idea make all Controller as a Service right?
  • Onema
    Onema about 10 years
    Like I said: "which one you use is up to your needs". Without knowing more about your specific project is difficult to advice how or what else you can do. You may need a base class or use a design pattern. I would just be consistent though out the code, so pick one and stick to it :). Please note that to me the way you are getting the EM in the constructor raises some red flags: the use of the $GLOBALS variable being one and the method chaining between multiple objects is another.