How to inject non-default entity managers?

d0001 picture d0001 · Oct 29, 2012 · Viewed 9.5k times · Source

In Symfony2 you can work with multiple entity managers and use something like the code below:

$em = $this->get('doctrine')->getManager();
$em = $this->get('doctrine')->getManager('default');

$customerEm =  $this->get('doctrine')->getManager('customer');

We can inject the default manager to any service by using:

"@doctrine.orm.entity_manager"

How can you inject non-default entity managers into services?

Answer

Mun Mun Das picture Mun Mun Das · Oct 29, 2012

If your entity managers config name is non_default then you can reference it as @doctrine.orm.non_default_entity_manager