U
U
ugin_root2019-10-15 06:26:47
Doctrine ORM
ugin_root, 2019-10-15 06:26:47

How to add a calculated field with logical operators in doctrine ORM?

I have a query like this:

$asins = $asinRepository->createQueryBuilder('asin')
    ->setParameter('master', $user->getId())
    ->addSelect('asin.isInWork AND asin.master = :master AS HIDDEN isInWorkSelf')
    ->addOrderBy('isInWorkSelf', 'desc')
    ->getQuery()
    ->getResult();

At run time, it throws an error:
[Syntax Error] line 0, col 27: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got 'AND'

This is the request generated by PHP:
SELECT 
    asin, 
    asin.isInWork AND asin.master = :master AS HIDDEN isInWorkSelf 
FROM App\Entity\Asin asin 
ORDER BY isInWorkSelf desc

I so understand DQL is not able to add a calculated field if in it there are logical operators?
Now I found this solution to this problem:
->addSelect('CASE WHEN asin.master = :master THEN asin.isInWork ELSE 0 END AS HIDDEN isInWorkSelf')

But it looks like a crutch.
Is there a way to make Doctrine ORM respond correctly to boolean operators on calculated fields?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question