Skip to content

Instantly share code, notes, and snippets.

@felipyamorim
Created September 17, 2020 19:19
Show Gist options
  • Select an option

  • Save felipyamorim/378b95da18184b4c96bee3faddf58b4d to your computer and use it in GitHub Desktop.

Select an option

Save felipyamorim/378b95da18184b4c96bee3faddf58b4d to your computer and use it in GitHub Desktop.
Doctrine Custom Top function
<?php
/**
* Created by PhpStorm.
* User: felipy.amorim
* Date: 10/03/2017
* Time: 13:05
*/
namespace App\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class TopFunction extends FunctionNode
{
public $quantityExpression = null;
public $selectExpression = null;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->quantityExpression = $parser->ArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->selectExpression = $parser->ArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return sprintf('TOP %s %s', $this->quantityExpression->dispatch($sqlWalker), $this->selectExpression->dispatch($sqlWalker));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment