mysql - 在 Symfony 2.5 和 Doctrine 查询生成器中使用圆括号 ( > )

标签 mysql function symfony doctrine-orm sum

我正在使用 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/

相关文章:

Mysql Alter表可以转换为默认值吗?

function - 如何在Haskell中的同一函数中使用多个 'commands'

c++ - 将重载函数分配给函数指针作为默认值

php - Symfony 自动加载命名空间问题

php - 具有非映射字段的 Symfony 3 唯一约束

mysql - mysql node js模块不关闭mysql连接会不会有什么问题

mysql - 如何查找多列的计数

两列之间的Mysql datetime

javascript - functions@lint 脚本失败

symfony - 获取未在 FormType 中声明的字段的值