title: Symfony2/Doctrine2 get joined entities from querybuilder object
body: <p>Given two doctrine entities (<code>Person</code> and <code>Company</code>), associated one-to-many, and a repository which looks something like this</p>
  
  <pre><code>namespace TestBundle\Entity\Repository;
  use Doctrine\ORM\EntityRepository;
  
  class PersonRepository extends EntityRepository {
  
      public function findAllByAge($age) {
          $qb = $this-&gt;createQueryBuilder('p')
                  -&gt;select('p','c')
                  -&gt;leftjoin('p.company', 'c')
                  -&gt;where("p.age = :age")
                  -&gt;setParameter('age', $age);
  
          // ...
      }
  }
  </code></pre>
  
  <p>How could I retrieve the entity (object or name) of the Company, preferably from the $qb object (or from the Alias, DQL, AST, parser, etc)?</p>
  
  <p>Ideally, I would like to have an array containing all the aliases used by the Querybuilder instance, or at least those defined in the <em>select</em> method, together with their entities, in the form:</p>
  
  <pre><code>[
      'p' =&gt; 'TestBundle\Entity\Person',
      'c' =&gt; 'TestBundle\Entity\Company',
      // etc
  ]
  </code></pre>
  
  <p>In <code>$qb-&gt;getDQLPart('join')</code> or even something lower level like <code>$qb-&gt;getQuery()-&gt;getAST()-&gt;fromClause-&gt;identificationVariableDeclarations</code> there's join information regarding the aliases, but it contains only the Root Entity and its alias (p = TestBundle\Entity\Person).</p>
  
  <p><code>getRootEntity</code>, <code>getRootAliases</code>, <code>getAllAliases</code> do not help as I get the root entity and/or all aliases, but no way to link them together.</p>
  
  <p><code>$em-&gt;getClassMetadata($rootentity)-&gt;associationMappings</code> gives me associations for the root entity, and it contains target entities for joins, but no aliases. I could map the field names to the information from <code>$qb-&gt;getDQLPart('join')</code> of course, but that would get me into an area where i'd have to crawl the information <em>recursively</em> from each entity object. That seems like it could cause serious errors.</p>
  
  <p>How does the Querybuilder translate the associations to the right entities? Or does it not do that at all, just parsing to the lower level stuff without ever knowing what entities it is using?</p>
  
  <p>I need the info so I can ensure certain entity fields have specific secondary indexes on them. These secondary indexes can be set using annotations, and are stored in the entity by doctrine (<code>$em-&gt;getClassMetadata($entity)-&gt;table['indexes']</code>). </p>
  
  <p>I need to know (programmatically) which fields have secondary indexes while building a query, and would prefer staying as high up the abstraction tree as possible.</p>