How to get doctrine2 table alias? How to get doctrine2 table alias? symfony symfony

How to get doctrine2 table alias?


The QueryBuilder object has two relevant methods:- getRootAliases()- getRootEntities()

Calling getRootAliases on a a QueryBuilder which has the from clause set will return an array of all aliases; similarly, calling getRootEntities will return an array of the shortened class names of the selected entities.

$qb = $em->createQueryBuilder();$qb->from('BundleName:EntityName', 'entityName');var_dump($qb->getRootAliases()); // returns ['entityName']


I managed to get alias with

 $alias = current($builder->getDQLPart('from'))->getAlias();

(where $builder is an instance of Doctrine\ORM\QueryBuilder)


You can retrive the select part of your QueryBuilder by calling the getDqlPart('select') method.

More information here.

Then, look how doctrine does to parse select part here.

You can probably do the same to know if the table associated to your repository is called and what is its alias.