Doctrine QueryBuilder: Problem using leftJoin in combination with addOrderBy

32 Views Asked by At

I am trying to understand an error I am having. This queryBuilder works as intended:

$alias = $queryBuilder->getRootAliases()[0];
$queryBuilder
     ->addOrderBy(
     sprintf(
           "CASE WHEN %s.product IS NULL THEN 0 ELSE 'ZZZZ' END",
           $alias,
     ),
     $value
);

As soon as I add a leftJoin before the orderBy I get the error "The alias "CASE WHEN o" does not exist in the QueryBuilder."

$alias = $queryBuilder->getRootAliases()[0];  
$queryBuilder
     ->leftJoin($alias . '.product','product')
     ->addOrderBy(
     sprintf(
           "CASE WHEN %s.product IS NULL THEN 0 ELSE 'ZZZZ' END",
           $alias,
     ),
     $value
);

This is my first question. Still learning. Any hints are greatly appreciated.

I tried creating the SQL queries manually but couldn't find the solution this way. I understand that everything before the dot is interpreted as an alias but I do not understand why this happens because of the join.

0

There are 0 best solutions below