Doctrine FIND_IN_SET leads to Error: Expected end

2019-09-13 05:29发布

I've got the following doctrine query in Symfony2:

$query = $em
    ->createQuery("
        SELECT m FROM MyBackendBundle:Merchant m
        WHERE m.active = :active
        ORDER BY FIND_IN_SET(m.range, 'all', 'without_special'), m.tradingAmount DESC
    ")
    ->setParameter('active', true)
;

But this leads to the following error:

[Syntax Error] line 0, col 112: Error: Expected end of string, got '('

and:

QueryException: 
SELECT m FROM My\Bundle\BackendBundle\Entity\Merchant m 
WHERE m.active = :active 
ORDER BY FIND_IN_SET(m.range, 'all', 'without_special') ASC, m.tradingAmount DESC

I use the FIND_IN_SET doctrine extension from beberlei to be able to use it in the query.

Any ideas why this happens?

Update:

Using the FIND_IN_SET in the SELECT as an alias this way:

SELECT m, FIND_IN_SET(m.range, 'all', 'without_special') AS HIDDEN findInSet
FROM MyBackendBundle:Merchant 
WHERE m.active = :active
ORDER BY findInSet, m.productAmount DESC

results in the following error:

[Syntax Error] line 0, col 56: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ','

1条回答
Luminary・发光体
2楼-- · 2019-09-13 05:53

Thanks to the comments of @qooplmao this is the working version:

$query = $em
    ->createQuery("
        SELECT m, FIND_IN_SET(m.range, 'all,without_special') AS rangeOrdering
        FROM MyBackendBundle:Merchant m
        WHERE m.active = :active
        ORDER BY rangeOrdering, m.tradingAmount DESC
    ")
    ->setParameter('active', true)
;

FIND_IN_SET has only two parameters and has to be within the SELECT part of the query as functions can't be used in the ORDER BY part of the query.

查看更多
登录 后发表回答