我正在使用 Symfony 2.5 和 Doctrine 开发一个小项目。我的查询在 MySQL Workbench 中完美运行。不幸的是,当我在我的查询构建器中使用括号时,在学说中出现以下错误:
QueryException:[语法错误] 第 0 行,第 19 列:错误:预期的 Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS,得到 '>'
$grades = $qb ->select(array( 'SUM(g.final > 89.5) as a', 'CONCAT (gcs.number, gcs.letter) as class' )) ->from('FicusEschoolBundle:Grade', 'g') ->leftJoin('g.course', 'gc') ->leftJoin('gc.schoolclass', 'gcs') ->where($qb->expr()->eq('gc.subject', $rid)) ->andWhere($qb->expr()->in('g.quarter', $filterQuarter)) ->groupBy('gc') ->orderBy('gcs.number') ->getQuery() ->getArrayResult();
最佳答案
默认情况下,Doctrine 不允许在聚合函数中有逻辑条件。您可以使用 beberlei/DoctrineExtensions
或者如果您不想安装整个库只需添加一个 IF
条件:https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/IfElse.php .
注册自定义 DQL 函数:
# app/config/config.yml
doctrine:
orm:
# ...
dql:
string_functions:
test_string: AppBundle\DQL\StringFunction
second_string: AppBundle\DQL\SecondStringFunction
numeric_functions:
test_numeric: AppBundle\DQL\NumericFunction
datetime_functions:
test_datetime: AppBundle\DQL\DatetimeFunction
来源:http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html
关于mysql - 在 Symfony 2.5 和 Doctrine 查询生成器中使用圆括号 ( > ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28801908/