我打算将IfFunction添加到DQL但它不起作用:
//My DQL Class
<?php
namespace Application\HappyBundle\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\Lexer;
/**
* IFFunction ::= "IF" "( "ArithmeticPrimary" , "ArithmeticPrimary" , "ArithmeticPrimary" )"
*/
class IFFunction extends FunctionNode
{
// (1)
public $firstNumericExpression = null;
public $secondNumericExpression = null;
public $thirdNumericExpression = null;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER); // (2)
$parser->match(Lexer::T_OPEN_PARENTHESIS); // (3)
$this->firstNumericExpression = $parser->ArithmeticPrimary(); // (4)
$parser->match(Lexer::T_COMMA); // (5)
$this->secondNumericExpression = $parser->ArithmeticPrimary(); // (6)
$parser->match(Lexer::T_COMMA); // (5)
//$parser->match(Lexer::T_CLOSE_PARENTHESIS); // (3)
$this->thirdNumericExpression = $parser->ArithmeticPrimary(); // (6)
$parser->match(Lexer::T_CLOSE_PARENTHESIS); // (3)
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'IF(' .
$this->firstNumericExpression->dispatch($sqlWalker) . ', ' .
$this->secondNumericExpression->dispatch($sqlWalker) . ', ' .
$this->thirdNumericExpression->dispatch($sqlWalker) .
')';
}
}
// config.yml中的Déclaration
dql:
datetime_functions:
timediff: Application\HappyBundle\DQL\TimeDiff
addtime: Application\HappyBundle\DQL\AddTime
numeric_functions:
IF: Application\HappyBundle\DQL\IFFunction
添加DQL函数有效,但是当我打算做例子时:
SELECT IF(1<2,'oui','non');
char'&lt;'上有错误 如果我把If函数放在这个
SELECT IF('1<2','oui','non');
这是工作,但不评估第一个条件:(
如果有人有想法...... 谢谢你的帮助。
答案 0 :(得分:6)
我找到了解决方案,问题在于解析器必须包含这样的ConditionnalExpression来评估:
class IfFunction extends FunctionNode
{
private $expr = array();
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->expr[] = $parser->ConditionalExpression();
for ($i = 0; $i < 2; $i++)
{
$parser->match(Lexer::T_COMMA);
$this->expr[] = $parser->ArithmeticExpression();
}
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return sprintf('IF(%s, %s, %s)',
$sqlWalker->walkConditionalExpression($this->expr[0]),
$sqlWalker->walkArithmeticPrimary($this->expr[1]),
$sqlWalker->walkArithmeticPrimary($this->expr[2]));
}
}