I am trying to get the number of people by age.
AGEBRACKET | NBR 10 | 3 20 | 14 30 | 123 40 | 4 50 | 55 ...
This is my code:
$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();
I get this error:
[Syntax error] row 0, column 7: Error: expected known function received 'Floor'
I was looking a bit for a solution, and here is what I found:
<?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";
And I get another error:
Attempted to call method "registerNumericFunction" on class "Doctrine\ORM\Query\Parser".
You know how I can do to get the desired result.
thanks
source share