Search code examples
phpsymfonysymfony-3.2

Access to entity manager in config.php


In app/config/config.php we can:

$container->loadFromExtension('doctrine', array(
    'dbal' => array(
        'driver'   => 'pdo_mysql',
        'host'     => '%database_host%',
        'dbname'   => '%database_name%',
        'user'     => '%database_user%',
        'password' => '%database_password%',
    ),
));

But is possible to get Entity Manager in this file?

$container->getDoctrine()

return:

Attempted to call an undefined method named "getDoctrine" of class "Symfony\Component\DependencyInjection\ContainerBuilder".

$container->get('doctrine')

return:

You have requested a non-existent service "doctrine"

$container->get('doctrine.orm.entity_manager')

return:

You have requested a non-existent service "doctrine.orm.entity_manager"


Solution

  • No this isn't possible. You can't access the services in the building step. You can use a CompilerPass to access services and call methods or aliases.

    Or if you just want to pass the EntityManager to an service, use the Reference class.