【发布时间】:2012-07-20 23:26:35
【问题描述】:
你好,在学说 2 中有 ifnull 吗? 我需要使用...如何?
SELECT *
FROM `apns_task_message`
ORDER BY IFNULL( `sent_at` , NOW( ) ) , `priority` DESC , `position` ASC
如何将这个sql转换成学说?
$qb = $this->getRepository()->createQueryBuilder('tm');
$qb->leftJoin('tm.apnsTask', 't');
$qb->add('where', 't.id = :task_id')->setParameter('task_id', $task_id);
//$qb->add('orderBy', 'IFNULL(tm.sent_at, NOW()), tm.priority DESC, tm.position ASC');
$qb->add('orderBy', 'd_date, tm.priority DESC, tm.position ASC');
$q = $qb->getQuery();
return $q->getResult();
找到了!!! 感谢@AdrienBrault 的“coalesce”运算符
$now = new \DateTime("now");
$qb = $this->getRepository()->createQueryBuilder('tm');
$qb->addSelect('coalesce (tm.sentAt, :sent_date) as sent_date')->setParameter('sent_date', $now->format("Y-m-d H:i:s"));
$qb->leftJoin('tm.apnsTask', 't');
$qb->add('where', 't.id = :task_id')->setParameter('task_id', $task_id);
$qb->add('orderBy', 'sent_date ASC, tm.priority DESC, tm.position ASC');
$q = $qb->getQuery();
【问题讨论】:
-
试试
COALESCE(tm.sent_at, NOW()) -
[Syntax Error] line 0, col 31: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got '('
-
COALESCE(tm.sent_at, CURRENT_DATE()) -
不起作用/[语法错误] 第 0 行,第 44 列:错误:预期的 Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS,得到 '('
-
怎么了?你设法解决了这个问题吗?
标签: symfony doctrine-orm ifnull