title: Symfony2: Doctrine MySql Math Functions
body: <p>I try to get count of persons by age bracket.</p>
  
  <pre><code>AGEBRACKET | NBR
      10     |  3
      20     |  14
      30     |  123
      40     |  4
      50     |  55
  ...
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>$qb = $em-&gt;createQueryBuilder();
      $qb-&gt;select('FLOOR((YEAR(CURDATE())-YEAR(p.date_birth)) / 10) * 10 AS age, COUNT(p.id)');
      $qb-&gt;from('MyBundle:Person', 'p');
      $qb-&gt;groupBy('age');
      $countByAge = $qb-&gt;getQuery()-&gt;execute();
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>[Syntax Error] line 0, col 7: Error: Expected known function, got
    'FLOOR'</p>
  </blockquote>
  
  <p>I look a little bit for a solution, and this is what have I found:</p>
  
  <pre><code>&lt;?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-&gt;walkSimpleArithmeticExpression(
              $this-&gt;simpleArithmeticExpression
          ) . ')';
      }
  
      public function parse(\Doctrine\ORM\Query\Parser $parser)
      {
          $lexer = $parser-&gt;getLexer();
  
          $parser-&gt;match(Lexer::T_IDENTIFIER);
          $parser-&gt;match(Lexer::T_OPEN_PARENTHESIS);
  
          $this-&gt;simpleArithmeticExpression = $parser-&gt;SimpleArithmeticExpression();
  
          $parser-&gt;match(Lexer::T_CLOSE_PARENTHESIS);
      }
  }
  
  &lt;?php
  \Doctrine\ORM\Query\Parser::registerNumericFunction('FLOOR', 'MyProject\Query\MysqlFloor');
  $dql = "SELECT FLOOR(person.salary * 1.75) FROM CompanyPerson person";
  </code></pre>
  
  <p>And I get another error:</p>
  
  <pre><code>Attempted to call method "registerNumericFunction" on class "Doctrine\ORM\Query\Parser".
  </code></pre>
  
  <p>Have you any idea how I can do to have the desired result. </p>
  
  <p>Thanks</p>