Doctrine 2 DQL MySQL相当于ROUND()? [英] Doctrine 2 DQL MySQL equivalent to ROUND()?

查看:108
本文介绍了Doctrine 2 DQL MySQL相当于ROUND()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文档中了解到:
http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#dql-functions
表示没有ROUND函数,但是没有编写自己的DQL类函数有简单的方法吗?

I know from the documentation at: http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#dql-functions that there is no ROUND function but is there an easy way to do it without writing my own DQL class function?

编辑:
我不会需要一个完全匹配,如果做一个平均值,并返回一个整数。

I would not need an exact match if doing an average and returning a whole number is possible.

推荐答案

你需要实现一个自定义DQL函数

DoctrineExtensions 中有一些示例

您可以执行如下:

<?php

namespace MyApp\DQL;

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

class Round extends FunctionNode
{
    private $arithmeticExpression;

    public function getSql(SqlWalker $sqlWalker)
    {

        return 'ROUND(' . $sqlWalker->walkSimpleArithmeticExpression(
            $this->arithmeticExpression
        ) . ')';
    }

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {

        $lexer = $parser->getLexer();

        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        $this->arithmeticExpression = $parser->SimpleArithmeticExpression();

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

然后您可以在配置中注册,同时引导ORM:

You can then register it in the configuration while bootstrapping the ORM:

$config = new \Doctrine\ORM\Configuration();

$config->addCustomNumericFunction('ROUND', 'MyApp\DQL\Round');

这篇关于Doctrine 2 DQL MySQL相当于ROUND()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆