【问题标题】:Symfony2: Doctrine MySql Math FunctionsSymfony2:Doctrine MySql 数学函数
【发布时间】:2015-08-01 19:50:07
【问题描述】:

我尝试按年龄段计算人数。

AGEBRACKET | NBR
    10     |  3
    20     |  14
    30     |  123
    40     |  4
    50     |  55
...

这是我的代码:

$qb = $em->createQueryBuilder();
    $qb->select('FLOOR((YEAR(CURDATE())-YEAR(p.date_birth)) / 10) * 10 AS age, COUNT(p.id)');
    $qb->from('MyBundle:Person', 'p');
    $qb->groupBy('age');
    $countByAge = $qb->getQuery()->execute();

我收到此错误:

[语法错误] line 0, col 7: Error: Expected known function, got '地板'

我有点寻找解决方案,这就是我发现的:

<?php
namespace MyProject\Query\AST;

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

class MysqlFloor extends FunctionNode
{
    public $simpleArithmeticExpression;

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return 'FLOOR(' . $sqlWalker->walkSimpleArithmeticExpression(
            $this->simpleArithmeticExpression
        ) . ')';
    }

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $lexer = $parser->getLexer();

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

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

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

<?php
\Doctrine\ORM\Query\Parser::registerNumericFunction('FLOOR', 'MyProject\Query\MysqlFloor');
$dql = "SELECT FLOOR(person.salary * 1.75) FROM CompanyPerson person";

我得到另一个错误:

Attempted to call method "registerNumericFunction" on class "Doctrine\ORM\Query\Parser".

您知道我该怎么做才能达到预期的效果吗?

谢谢

【问题讨论】:

    标签: php mysql symfony doctrine-orm


    【解决方案1】:

    Doctrine 文档中有一个更新版本可以帮助您: http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language

    如果你想将它添加到你的 Symfony 配置中以便它可以在你的项目中的任何地方使用,请参阅http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html 了解如何做到这一点。

    【讨论】:

      【解决方案2】:

      解决办法:

      #config.yml
          orm:
              dql:
                  numeric_functions:
                      FLOOR: FrontBundle\DoctrineFunctions\FloorFunction
      

      #FloorFunction.php
      <?php
      namespace MyBundle\DoctrineFunctions;
      
      use \Doctrine\ORM\Query\AST\Functions\FunctionNode;
      use \Doctrine\ORM\Query\Lexer;
      
      class FloorFunction extends FunctionNode
      {
          public $simpleArithmeticExpression;
      
          public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
          {
              return 'FLOOR(' . $sqlWalker->walkSimpleArithmeticExpression(
                  $this->simpleArithmeticExpression
              ) . ')';
          }
      
          public function parse(\Doctrine\ORM\Query\Parser $parser)
          {
              $parser->match(Lexer::T_IDENTIFIER);
              $parser->match(Lexer::T_OPEN_PARENTHESIS);
      
              $this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression();
      
              $parser->match(Lexer::T_CLOSE_PARENTHESIS);
          }
      }
      

      $config = $em->getConfiguration();
      $config->addCustomNumericFunction('FLOOR', 'MyBundle\DoctrineFunctions\FloorFunction');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-07
        • 2016-02-11
        • 2013-03-11
        • 1970-01-01
        相关资源
        最近更新 更多