Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

More about Container, the “doctrine” Service and the Entity Manager

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

More about Container, the “doctrine” Service and the Entity Manager

In our test, we needed Doctrine’s entity manager and to get it, we used Symfony’s container. Remember from the first episode in this series that the “container” is basically just a big array filled with useful objects. To get a list of all of the objects in the container, run the container:debug console command:

php app/console container:debug

One of these objects is called doctrine, which is an instance of a class called Registry:

doctrine    container    Doctrine\Bundle\DoctrineBundle\Registry

So when we say $container->get('doctrine'), we’re getting this object.

Find the shortcut in your editor that can open files by typing in their filename. Use this to find and open Registry.php. Inside, you’ll see the getManager method being used, which actually lives on its parent class.

The Base Controller

So how does this compare with how we normally get the entity manager in a controller? Open up RegisterController. That’s right - in a controller, we always say $this->getDoctrine()->getManager():

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

The getDoctrine() method lives inside Symfony’s Controller class, which we’re extending. Let’s open up that class to see what this method does:

// vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
public function getDoctrine()
    return $this->container->get('doctrine');

Ah-hah! The getDoctrine method is just a shortcut to get the service object called doctrine from the container. This means that no matter where we are, the process to get the entity manager is always the same: get the container, get the doctrine service, then call getManager on it.

My big point is that if you have the container, then you can get any object in order to do any work you need to. The only tricky part is knowing what the name of the service object is and what methods you can call on it. Using the app/console container:debug task can help.

Leave a comment!

Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4", // v2.4.2
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.0.1
        "symfony/assetic-bundle": "~2.3", // v2.3.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.5
        "symfony/monolog-bundle": "~2.4", // v2.5.0
        "sensio/distribution-bundle": "~2.3", // v2.3.4
        "sensio/framework-extra-bundle": "~3.0", // v3.0.0
        "sensio/generator-bundle": "~2.3", // v2.3.4
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
        "ircmaxell/password-compat": "~1.0.3", // 1.0.3
        "phpunit/phpunit": "~4.1" // 4.1.0