Joh*_*hny 5 mysql sum function symfony doctrine-orm
我正在使用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();
Run Code Online (Sandbox Code Playgroud)默认情况下,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
Run Code Online (Sandbox Code Playgroud)
来源:http ://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html