Doctrine 2 DQL MySQL equivalent to ROUND()?

前端 未结 3 919
南笙
南笙 2020-12-19 04:35

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 the

3条回答
  •  渐次进展
    2020-12-19 05:13

    You need to implement a custom DQL function for that.

    There's some examples in DoctrineExtensions.

    You can implement it like following:

    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);
        }
    }
    

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

    $config = new \Doctrine\ORM\Configuration();
    
    $config->addCustomNumericFunction('ROUND', 'MyApp\DQL\Round');
    

提交回复
热议问题