【问题标题】:CASE WHEN not working doctrine2 query builder案例不工作的学说2查询生成器
【发布时间】:2015-09-04 06:21:14
【问题描述】:

我编写了以下代码来使用查询构建器构建查询。

            $em = $this->_em;
            $qb = $em->createQueryBuilder();
            $qb->select('tbl');
            $qb->addSelect('COUNT(tbl.macId) AS totalInstallations');
            $qb->addSelect('COUNT(DISTINCT tbl.macId) AS uniqueInstallations');
            $qb->addSelect('COUNT(CASE  
                             WHEN tbl.updatedOn IS NOT NULL THEN tbl.macId ELSE NULL
                            END) AS totalUninstallations');
            $qb->from('Entity\SoftwareInstallation', 'tbl');
            $result = $qb->getQuery()->getArrayResult();
            return $result;

但在这种情况下它不起作用。

我收到以下错误:

类型:Doctrine\ORM\Query\QueryException

消息:[语法错误] 第 0 行,第 152 列:错误:预期 Doctrine\ORM\Query\Lexer::T_FROM,得到 '.'

文件名: /var/www/html/ghostnew/application/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php

行号:52

【问题讨论】:

    标签: doctrine-orm query-builder


    【解决方案1】:

    尝试使用SUM() 而不是COUNT()

    看到这个答案:Doctrine 2 DQL CASE WHEN in Count

    【讨论】:

    • 虽然在您的情况下用 SUM 替换 COUNT 有效,但在 DQL 中使用 CASE 部分似乎存在一般问题。我在这里打开了一个问题:github.com/doctrine/doctrine2/issues/5915随时订阅。
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2011-10-10
    • 1970-01-01
    • 2012-12-07
    • 2016-04-02
    • 2013-09-29
    相关资源
    最近更新 更多