Isaac Sampaio
Isaac Sampaio

Reputation: 31

Inject EntityManager into Dependendy Injection symfony

I'm trying to change this structure boot of my entity manager to use Symfony component dependendy injection.

Currently initialize it.

$paths = array(__DIR__ . "/app/Entity/");

$isDevMode = true;

$conn = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => 'senha',
    'dbname'   => 'db_teste',
    'host'     => 'localhost',
    'port'     => '3306',  
); 

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$cachingBackend = new \Doctrine\Common\Cache\FilesystemCache('/tmp/doctrine2');
$config->setMetadataCacheImpl($cachingBackend);
$config->setQueryCacheImpl($cachingBackend);
$config->setResultCacheImpl($cachingBackend); 

$em = EntityManager::create($conn, $config);

At this point I already have my entitymanager available for use.

Now I started my container.

$container = new ContainerBuilder();

But I do not know how to inject the entitymanager as a service.

I've read enough documentation, but there does not show how to do this when we have the situation of static methods.

Someone help me?

Thanks!

Upvotes: 2

Views: 145

Answers (1)

Markus Kottländer
Markus Kottländer

Reputation: 8268

What about:

...
// by looking into the EntityManager class, I see that the
// constructor takes a mandatory eventManager in contrary
// to the static create method, where the EventManager is optional.
$eventManager = new Doctrine\Common\EventManager();

$container->register('entityManager', 'Doctrine\ORM\EntityManager')
->setArguments(array($conn, $config, $eventManager));

Upvotes: 0

Related Questions