Skip to content

Instantly share code, notes, and snippets.

@galiazzi
Last active May 7, 2024 12:04
Show Gist options
  • Save galiazzi/5e5f04f9753ba4d8a9b972c87dc2a805 to your computer and use it in GitHub Desktop.
Save galiazzi/5e5f04f9753ba4d8a9b972c87dc2a805 to your computer and use it in GitHub Desktop.
Doctrine DQL cast function
<?php
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class Cast extends FunctionNode
{
private $expr1;
private $expr2;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->expr1 = $parser->ArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->expr2 = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
$type = trim($this->expr2->dispatch($sqlWalker), "'");
return sprintf(
'cast(%s as %s)',
$this->expr1->dispatch($sqlWalker),
$type
);
}
}
@galiazzi
Copy link
Author

thanks @lyrixx

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