Created
September 8, 2016 05:24
-
-
Save olimsaidov/4bbd530b1b645ce75e1bbb781b5dd91f to your computer and use it in GitHub Desktop.
Doctrine Greatest Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use Doctrine\ORM\Query\AST\Node; | |
use Doctrine\ORM\Query\Lexer; | |
use Doctrine\ORM\Query\Parser; | |
use Doctrine\ORM\Query\SqlWalker; | |
class Greatest extends FunctionNode | |
{ | |
public $exprs; | |
public function parse(Parser $parser) | |
{ | |
$this->exprs = []; | |
$lexer = $parser->getLexer(); | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->exprs[] = $parser->ArithmeticPrimary(); | |
while (Lexer::T_COMMA === $lexer->lookahead['type']) { | |
$parser->match(Lexer::T_COMMA); | |
$this->exprs[] = $parser->ArithmeticPrimary(); | |
} | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
public function getSql(SqlWalker $sqlWalker) | |
{ | |
return 'GREATEST(' . join(',', array_map(function(Node $expr) use ($sqlWalker) { | |
return $expr->dispatch($sqlWalker); | |
}, $this->exprs)) . ')'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could be using in symfony using http://symfony.com/doc/current/doctrine/custom_dql_functions.html