With the following query builder:
$qb = $this->createQueryBuilder('t');
$qb
->select('t, a, m, s')
->leftJoin('t.author', 'a')
->leftJoin('t.messages', 'm')
->leftJoin('t.server', 's')
;
// STATE_* are integers.
$qb->where('t.state != :state')->setParameter('state', Ticket::STATE_CLOSE);
$qb->addSelect('CASE WHEN (t.assigned IS NULL) THEN -10 + t.state ELSE t.state AS state_order');
$qb->addOrderBy('state_order', 'ASC');
$qb->addOrderBy('t.priority', 'DESC');
$qb->addOrderBy('t.addedAt', 'ASC');
// $assignee is a User entity instance.
$qb->andWhere('t.assigned = :assigned')->setParameter('assigned', $assignee);
$qb->andWhere($qb->expr()->in('t.state', [
Ticket::STATE_OPEN,
Ticket::STATE_PENDING,
Ticket::STATE_PENDING,
]));
I have this error on execute():
[Syntax Error] line 0, col 82: Error: Expected Doctrine\ORM\Query\Lexer::T_END, got 'AS'
Here is the produced DQL:
SELECT t, a, m, s, CASE WHEN (t.assigned IS NULL) THEN -10 + t.state ELSE t.state AS state_order FROM AppBundle\Entity\Ticket t LEFT JOIN t.author a LEFT JOIN t.messages m LEFT JOIN t.server s WHERE t.state != :state AND t.assigned = :assigned AND t.state IN(0, 1, 1) ORDER BY state_order ASC, t.priority DESC, t.addedAt ASC
Rolling back to 2.5 solve the issue, so it looks like a BC break.
This is actually bug in your code: CASE WHEN ... THEN ... ELSE ... requires to be ended with END. For reference see DQL EBNF for GeneralCaseExpression in 2.5: https://github.com/doctrine/doctrine2/blob/v2.5.14/docs/en/reference/dql-doctrine-query-language.rst#case-expressions
The code for this hasn't been touched for years though so any breakage in this regard is suspicious.
@Majkl578 I'll take a look, but this change nothing.
This was perfectly working with the same code for v2.5, not after upgrading to 2.6.
Rolling back to v2.5 was the only thing I did to make it working again.
I confirm that changing to since:
$qb->addSelect('CASE WHEN (t.assigned IS NULL) THEN -10 + t.state ELSE t.state END AS state_order');
solved the issue, thanks.
But I maintain this is working on 2.5. Event if it should not work, a deprecation message should be thrown instead IMHO.
This looks like to be a bug in 2.5 syntax parser, 2.6 fixed it.
ran into the same issue when upgrading 2.5 -> 2.6. Solution described above (using the end keyword) worked.
Most helpful comment
This is actually bug in your code:
CASE WHEN ... THEN ... ELSE ...requires to be ended withEND. For reference see DQL EBNF for GeneralCaseExpression in 2.5: https://github.com/doctrine/doctrine2/blob/v2.5.14/docs/en/reference/dql-doctrine-query-language.rst#case-expressionsThe code for this hasn't been touched for years though so any breakage in this regard is suspicious.