Answer the question
In order to leave comments, you need to log in
How to check if it is possible to join on a table before the join in DQL?
/**
* @throws JoinIsNotSupportedException
* @throws ReflectionException
*/
protected function innerJoinTablesIfNotJoined(
QueryBuilder $query,
string $joinToTableName,
array $checkTablesNamesAliases
): QueryBuilder {
$joinedAliases = $query->getAllAliases();
$firstSelectFromTableClass = $query->getRootEntities()[0];
$paramList = $this->getObjectPropertiesNames($firstSelectFromTableClass);
foreach ($checkTablesNamesAliases as $joinTableAlias) {
if (false === in_array($joinTableAlias, $paramList, true)) {
throw JoinIsNotSupportedException::joinNotSupported($joinToTableName, $joinTableAlias, $paramList);
}
if (false === \in_array($joinTableAlias, $joinedAliases, true)) {
$query->join(\sprintf('%s.%s', $joinToTableName, $joinTableAlias), $joinTableAlias);
}
}
return $query;
}
$query->getRootEntities()[0];
will return only the root entity, the main table from which the selection is made and to which the joins. If we have the 2nd join, for example posts -> join -> user
will work correctly firstSelectFromTableClass === 'posts' Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question