Given two doctrine entities (Person
and Company
), associated one-to-many, and a repository which looks something like this
namespace TestBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class PersonRepository extends EntityRepository {
public function findAllByAge($age) {
$qb = $this->createQueryBuilder('p')
->select('p','c')
->leftjoin('p.company', 'c')
->where("p.age = :age")
->setParameter('age', $age);
// ...
}
}
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)?
Ideally, I would like to have an array containing all the aliases used by the Querybuilder instance, or at least those defined in the select method, together with their entities, in the form:
[
'p' => 'TestBundle\Entity\Person',
'c' => 'TestBundle\Entity\Company',
// etc
]
In $qb->getDQLPart('join')
or even something lower level like $qb->getQuery()->getAST()->fromClause->identificationVariableDeclarations
there's join information regarding the aliases, but it contains only the Root Entity and its alias (p = TestBundle\Entity\Person).
getRootEntity
, getRootAliases
, getAllAliases
do not help as I get the root entity and/or all aliases, but no way to link them together.
$em->getClassMetadata($rootentity)->associationMappings
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 $qb->getDQLPart('join')
of course, but that would get me into an area where i'd have to crawl the information recursively from each entity object. That seems like it could cause serious errors.
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?
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 ($em->getClassMetadata($entity)->table['indexes']
).
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.