Check if an alias exists?

How can I check if an alias is already defined?

There is no way to verify this.

This does not work because I only have a ROOT alias and no connection aliases.

/** @var $query \Doctrine\ORM\QueryBuilder */ $query->getRootAliases() 

Parts are another unacceptable option for me ...

 $query->getDQLPart() 

Returns only parts such as "select, from, etc.".

And I would "ignore" this mistake.

 [Semantical Error] line 0, col 254 near '_user LEFT': Error: '_user' is already defined. 

How to check if an alias exists?

+6
source share
2 answers

You get this error because you are trying to join an already defined alias.

The solution is to test the connections of the DQL parts. In your repository method, you can do something like this:

 $joinDqlParts = $queryBuilder->getDQLParts()['join']; $aliasAlreadyExists = false; /* @var $join Query\Expr\Join */ foreach ($joinDqlParts as $joins) { foreach ($joins as $join) { if ($join->getAlias() === '_user') { $aliasAlreadyExists = true; break 2; } } } if ($aliasAlreadyExists === false) { $queryBuilder->innerJoin('parenttable._user', '_user') } 

So, you join only if you have not joined.

+6
source

You can use the PHP in_array built-in function to check if a value exists in the array or not.

 in_array($alias, $qb->getAllAliases()) 
+5
source

All Articles