Skip to content

Instantly share code, notes, and snippets.

@edwinyoyada
Created November 24, 2016 08:30
Show Gist options
  • Save edwinyoyada/03fadcc77121e304cc586e9b00af63fd to your computer and use it in GitHub Desktop.
Save edwinyoyada/03fadcc77121e304cc586e9b00af63fd to your computer and use it in GitHub Desktop.
Doctrine SQL If Function
<?php
namespace Your\PathToBundle\DQL;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* Created by PhpStorm.
* User: Edwin Yoyada Pratama
* Date: 11/24/2016
* Time: 12:19 PM
*/
class IfFunction extends FunctionNode {
public $firstStringExpression = null;
public $secondStringExpression = null;
public $isTrue = null;
public $isFalse = null;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->firstStringExpression = $parser->SimpleEntityExpression();
$parser->match(Lexer::T_EQUALS);
$this->secondStringExpression = $parser->StringPrimary();
$parser->match(Lexer::T_COMMA);
$this->isTrue = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->isFalse = $parser->SimpleArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return "IF(" . $this->firstStringExpression->dispatch($sqlWalker) . " = " . $this->secondStringExpression->dispatch($sqlWalker) . ", " . $this->isTrue->dispatch($sqlWalker) . ", " . $this->isFalse->dispatch($sqlWalker) . ")";
}
}
@albertlieyingadrian
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment