0
我寫了一個自定義函數的DQL:學說2 where條件沒有比較
<?php namespace Bundle\DQL\Functions;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\Parser;
/**
* "DATE_COMPARE" "(" ArithmeticPrimary "," ComparisonOperator "," ArithmeticPrimary ")"
*/
class DateCompareFunction extends FunctionNode
{
public $date1;
public $date2;
public $operator;
/**
* @override
* @param SqlWalker $sqlWalker
* @return string
* @throws \Doctrine\DBAL\DBALException
*/
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'TRUNC(%s) %s TRUNC(%s)',
$this->date1->dispatch($sqlWalker),
$this->operator,
$this->date2->dispatch($sqlWalker)
);
}
/**
* @override
* @param Parser $parser
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->date1 = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->operator = $parser->ComparisonOperator();
$parser->match(Lexer::T_COMMA);
$this->date2 = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
而且我在那裏語句是這樣的:
$query = $em->createQueryBuilder()
->select('evt')
->from('Application\Model\Event', 'evt')
->where('evt.USR_ID in (:uid)')
->setParameter('uid', $usersId);
if (null !== $from) {
$query->andWhere('DATE_COMPARE(evt.DAY, >, TO_DATE(:from, \'yyyy-mm-dd\'))')
->setParameter('from', $from);
的問題是,原則提高對異常有WHERE
聲明沒有比較符號:
object(Doctrine\ORM\Query\QueryException)[347]
protected 'message' => string '[Syntax Error] line 0, col 130: Error: Expected =, <, <=, <>, >, >=, !=, got ')'' (length=80)
private 'string' (Exception) => string '' (length=0)
protected 'code' => int 0
protected 'file' => string 'C:\Workspace\app\hcalendar\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php' (length=88)
protected 'line' => int 52
private 'trace' (Exception) =>
我已經嘗試添加一個stmt = TRUE
,但生成的語句不被Oracle理解,我可以在沒有任何比較符號的情況下執行where語句嗎? (只是一個真/假功能返回)
這就是我實現的解決方案:) – kitensei 2014-10-14 06:12:04