【问题标题】:using custom DQL function in queries in Symfony2在 Symfony2 的查询中使用自定义 DQL 函数
【发布时间】:2013-10-29 08:50:06
【问题描述】:

我添加了以下查询扩展:

<?php

/*
 * DoctrineExtensions Mysql Function Pack
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to kontakt@beberlei.de so I can send you a copy immediately.
 */

namespace MyApp\MainBundle\DQL;

use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;

/**
 * "DAY" "(" SimpleArithmeticExpression ")". Modified from DoctrineExtensions\Query\Mysql\Year
 *
 * @category    DoctrineExtensions
 * @package     DoctrineExtensions\Query\Mysql
 * @author      Rafael Kassner <kassner@gmail.com>
 * @author      Sarjono Mukti Aji <me@simukti.net>
 * @license     MIT License
 */
class Day extends FunctionNode
{
    public $date;

    /**
     * @override
     */
    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return "DAY(" . $sqlWalker->walkArithmeticPrimary($this->date) . ")";
    }

    /**
     * @override
     */
    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        $this->date = $parser->ArithmeticPrimary();

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }
}

我想在一个组中使用它,所以我做了一个查询:

->addGroupBy('DAY(v.created)')  

但是它总是给我一个错误:

[Semantical Error] line 0, col 90 near 'DAY(v.create': Error: Cannot group by undefined identification or result variable.

这是为什么?

【问题讨论】:

    标签: symfony orm doctrine-orm


    【解决方案1】:

    自从这次提交:https://github.com/doctrine/doctrine2/commit/2642daa43851878688d01625f272ff5874cac7b2

    这个问题通过 SelectExpression + Hidden + ResultVariable 参考解决。

    SELECT 
        ..., DAY(t.scheduledDate) AS myDay
    FROM Task t
        ...
    WHERE
        ...
    GROUP BY
        myDay
    

    【讨论】:

      【解决方案2】:

      如果要对其进行 GROUP BY(或 ORDER BY),则必须在 SELECT 部分中添加计算值使用关键字 HIDDEN 在获取的结果中忽略该字段!

      $qb
          // select...
          ->addSelect('DAY(v.created) AS HIDDEN myGroupByField')
          // ...
          ->groupBy('myGroupByField')
      ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-02
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多