Skip to content

Instantly share code, notes, and snippets.

@K-Phoen
Created July 29, 2013 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K-Phoen/6108436 to your computer and use it in GitHub Desktop.
Save K-Phoen/6108436 to your computer and use it in GitHub Desktop.
MySQL REPLACE() for Doctrine2
<?php
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* REPLACE(str, from_str, to_str)
* "REPLACE" "(" StringPrimary "," StringPrimary "," StringPrimary ")"
*/
class Replace extends FunctionNode
{
protected $stringStr, $stringFromStr, $stringToStr;
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'REPLACE(%s, %s, %s)',
$sqlWalker->walkStringPrimary($this->stringStr),
$sqlWalker->walkStringPrimary($this->stringFromStr),
$sqlWalker->walkStringPrimary($this->stringToStr)
);
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER); // REPLACE
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->stringStr = $parser->StringPrimary(); // str
$parser->match(Lexer::T_COMMA);
$this->stringFromStr = $parser->StringPrimary(); // from_str
$parser->match(Lexer::T_COMMA);
$this->stringToStr = $parser->StringPrimary(); // to_str
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment