Doctrine FIND_IN_SET导致错误:字符串的预期结束,得到了'('

时间:2016-03-27 10:05:52

标签: php symfony doctrine doctrine-extensions

我在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)
;

但这会导致以下错误:

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

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

我使用FIND_IN_SET doctrine extension from beberlei可以在查询中使用它。

为什么会发生这种情况?

更新

使用SELECT中的FIND_IN_SET作为别名:

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

导致以下错误:

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

1 个答案:

答案 0 :(得分:1)

感谢@qooplmao的评论,这是工作版本:

$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只有两个参数,必须位于查询的SELECT部分,因为函数无法在查询的ORDER BY部分使用。