title: Symfony2/Doctrine2: Issues managing multiple entity manager inside a listener
body: <p>i'm following that recipe  <code>http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/cookbook/blending-orm-and-mongodb-odm.html#event-subscriber</code> and when i get to the event subscriber i'm not able to inject the proper <code>entity manager</code>, the one named <code>$this-&gt;dm</code> initialized in the constructor.  </p>
  
  <p>As i understand, the entity manager used by the entity which is being loaded can be retrieved through <code>$em = $eventArgs-&gt;getEntityManager();</code> then i need another one which i <code>inject</code> in the following manner:  </p>
  
  <pre>
      services:
          postload.listener:
          class: myVendor\myFooBarBundle\Listener\myEntityListener
          tags:
              - { name: doctrine.event_listener, event: postLoad }
          arguments:
              - "@doctrine.orm.foobar_entity_manager"
  </pre>
  
  <p>Those are my entity managers:  </p>
  
  <pre>
  //orm.yml
          orm:
          entity_managers:
              default:
                  connection: default
                  mappings:
                      myVendormyFooBarBundle:
                          prefix: "myVendor\myFooBarBundle\Entity"
                          type: annotation
                          is_bundle: true
                          dir: "Entity"
              foobar:
                  connection: foobar
                  mappings:
                      myVendormyFooBarBundle:
                          prefix: "myVendor\myFooBarBundle\View"
                          type: annotation
                          is_bundle: true
                          dir: "View"
  </pre>
  
  <p>When <code>injecting</code> the <code>foobar entity manager</code> using the above strategy i get the following error:  </p>
  
  <pre><code>Circular reference detected for service "postload.listener", path: "routing.loader -&gt; routing.db.loader -&gt; doctrine.orm.default_entity_manager -&gt; doctrine.dbal.default_connection -&gt; postload.listener -&gt; doctrine.orm.fooba_entity_manager -&gt; doctrine.dbal.foobar_connection".  
  </code></pre>
  
  <p>That's <code>myVendor\myFooBarBundle\Listener\myEntityListener</code> class:</p>
  
  <pre>
      class myFooBarEntityListener
      {
  
          public function __construct( \Doctrine\ORM\EntityManager $em )
          {
              $this->em = $em;
          }
  
          public function postLoad( LifecycleEventArgs $eventArgs )
          {
              $myEntity = $eventArgs->getEntity();
  
              if( $myEntity instanceof \myVendor\myFooBarBundle\Entity\myEntity )
              {
                  $em = $eventArgs->getEntityManager();
                  $fooBarReflProp = $em->getClassMetadata( 'myVendor\myFooBarBundle\Entity\myEntity' )->reflClass->getProperty( 'FooBarEntity' );
                  $fooBarReflProp->setAccessible( true );
                  $fooBarEntity = $this->em->getRepository( 'myVendor\myFooBarBundle\View\myFooBarEntity' )->findOneBy( array( 'id' => $myEntity->getFooBarEntityId() ) );
                  $fooBarReflProp->setValue( $myEntity, $fooBarEntity );
              }
          }
      }
  </pre>
  
  <p>Also to avoid the <code>circular reference error</code> i've tried <code>not</code> to inject the <code>foobar entity manager</code> and get it  through <code>LifecycleEventArgs $eventArgs</code>:</p>
  
  <pre>
      class myFooBarEntityListener
      {
  
          public function postLoad( LifecycleEventArgs $eventArgs )
          {
              $myEntity = $eventArgs->getEntity();
  
              if( $myEntity instanceof \myVendor\myFooBarBundle\Entity\myEntity )
              {
                  $em = $eventArgs->getEntityManager();
                  $fooBarReflProp = $em->getClassMetadata( 'myVendor\myFooBarBundle\Entity\myEntity' )->reflClass->getProperty( 'FooBarEntity' );
                  $fooBarReflProp->setAccessible( true );
                  //NOTICE HOW HERE I SHOULD GET THE PROPER ENTITY MANAGER THROUGH $eventArgs
                  $fooBarEntity = $eventArgs->getEntityManager('foobar')->getRepository( 'myVendor\myFooBarBundle\View\myFooBarEntity' )->findOneBy( array( 'id' => $myEntity->getFooBarEntityId() ) );
                  $fooBarReflProp->setValue( $myEntity, $fooBarEntity );
              }
          }
      }
  
  </pre>
  
  <p>That last implementation throughs me the following error:  </p>
  
  <p><code>An exception has been thrown during the rendering of a template ("Class myVendor\myFooBarBundle\View\myFooBarEntity is not a valid entity or mapped super class.") in "SonataAdminBundle:CRUD:base_list.html.twig" at line 28.</code>  </p>
  
  <p>The above error is caused by <code>$fooBarEntity = $eventArgs-&gt;getEntityManager('foobar')-&gt;getRepository( 'myVendor\myFooBarBundle\View\myFooBarEntity' )-&gt;findOneBy( array( 'id' =&gt; $myEntity-&gt;getFooBarEntityId() ) );</code> because when i place echo 'hello';die(); just before that line  the error is not thrown but when placed just after the line the error is thrown and <code>hello</code> is not shown. The error makes me think that although i'm explicitly getting the <code>foobar</code> connection through <code>$eventArgs-&gt;getEntityManager('foobar')</code> it is still giving me the <code>default</code> <code>connection/entity manager</code>.</p>
  
  <p>In order to double check <code>myVendor\myFooBarBundle\View\myFooBarEntity</code> syntax i went to <code>octrine\ORM\Mapping\Driver\DriverChain</code> and placed the following code:</p>
  
  <pre>
      if( strpos( $className, 'myFooBarEntity' ) )
      {
          echo 'Class: '.$className."\n\n";
          foreach ($this->_drivers as $namespace => $driver)
          {
              echo 'namespace: '.$namespace."\n";
              $bool = strpos($className, $namespace);
              var_dump($bool);
              echo "\n\n";
          }
      }
      die();
  </pre>
  
  <p>That DriverChain code gives me the following, that's why i think 'foobar' connection is never used or symfony has some kind of bug interpreting <code>orm.yml</code> file which defines the entity managers plus namespaces to use.</p>
  
  <p>
  class: myVendor\myFooBarBundle\View\myFooBarEntity</p>
  
  <p>namespace: myVendor\myFooBarBundle\Entity
  bool(false) 
  </p>
  
  <p>If i look foor the <code>entity</code> word inside <code>myVendor\myFooBarBundle\View\myFooBarEntity</code> i just find <code>@ORM\Entity</code> for the entity definition and also <code>@ORM\OneToMany( targetEntity=.....)</code> for the relation with another entity.</p>
  
  <p>I hope someone can help, as it's driving me crazy. Thanks a lot!!</p>