你需要声明第二个参数,并像这样使用Lexer:
namespace HQF\Bundle\PizzasBundle\DQL;
use \Doctrine\ORM\Query\AST\Functions\FunctionNode;
use \Doctrine\ORM\Query\Lexer;
class MysqlRound extends FunctionNode
{
private $firstExpression = null;
private $secondExpression = null;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$lexer = $parser->getLexer();
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->firstExpression = $parser->ArithmeticPrimary();
// parse second parameter if available
if(Lexer::T_COMMA === $lexer->lookahead['type']){
$parser->match(Lexer::T_COMMA);
$this->secondExpression = $parser->ArithmeticPrimary();
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
// use second parameter if parsed
if (null !== $this->secondExpression){
return 'ROUND('
. $this->firstExpression->dispatch($sqlWalker)
. ', '
. $this->secondExpression->dispatch($sqlWalker)
. ')';
}
return 'ROUND(' . $this->firstExpression->dispatch($sqlWalker) . ')';
}
}
编辑
许多Doctrine2扩展已被写入here.所有学分都归功于@beberlei的出色工作.许多功能都可用(IFELSE,IFNULL,NULLIF,COS,ACOS等等)但不是全部(ROUND,GREATEST或LEAST都缺失了,但如果需要,你仍然可以自己编写它们.)
如果觉得《php自定义函数指定参数类型 php – Doctrine:如何添加带可选参数的自定义函数?...》对你有帮助,请点赞、收藏,并留下你的观点哦!