- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,在 doctrine2 中有 ifnull 吗?我需要使用...如何使用?
SELECT *
FROM `apns_task_message`
ORDER BY IFNULL( `sent_at` , NOW( ) ) , `priority` DESC , `position` ASC
如何把这个sql转成doctrine?
$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 的“合并”运算符
$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();
最佳答案
如果你想在doctrine query builder中使用sql的IFNULL函数,你需要使用doctrine extension for IFNULL。
首先你需要注册扩展类以在 Doctrine bootstrap 中使用
$config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
$driver = new AnnotationDriver(new AnnotationReader());
$config->setMetadataDriverImpl($driver);
require_once './Doctrine/DoctrineExtensions/Query/MySql/IfNull.php';
$config->addCustomStringFunction('IfNull', '\DoctrineExtensions\Query\Mysql\IfNull');
$this->em = EntityManager::create($connection_options, $config);
$classLoader = new ClassLoader('DoctrineExtensions', $extension_dir);
$classLoader->register();
这里是IFNULL原则扩展代码。
<?php
namespace DoctrineExtensions\Query\Mysql;
use Doctrine\ORM\Query\AST\Functions\FunctionNode,
Doctrine\ORM\Query\Lexer;
/**
* @author Andrew Mackrodt <andrew@ajmm.org>
*/
class IfNull extends FunctionNode
{
private $expr1;
private $expr2;
public function parse(\Doctrine\ORM\Query\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->ArithmeticExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'IFNULL('
.$sqlWalker->walkArithmeticPrimary($this->expr1). ', '
.$sqlWalker->walkArithmeticPrimary($this->expr2).')';
}
}
您可以从 here 下载扩展
对于你的情况,使用这个。
$qb = $this->getRepository()->createQueryBuilder('tm');
return $qb
->select("*")
->from("tm.apnsTask", "t")
->orderBy("IFNULL(t.sent_at, NOW()), t.priority DESC, t.position ASC")
->getQuery()
->getResult();
关于symfony - doctrine2 ifnull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11600468/
如果我使用过程编程语言,我会使用类似 sys.MAX_INT 的东西来代替 999999。SQL 中是否存在这样的东西?或者是否有更简洁的方法来摆脱此 SQL 中的 999999? SELECT
嗨,在 doctrine2 中有 ifnull 吗?我需要使用...如何使用? SELECT * FROM `apns_task_message` ORDER BY IFNULL( `sent_at`
我有以下数据集: { patientId: 228, medication: { atHome : [ { "drug" : "
我如何发表这个声明AND IFNULL ? TIMESTAMPDIFF(YEAR,a.date_of_birth,CURDATE()) >= '' AND TIMESTAMPDIFF(YEAR,a.d
当 MySQL SELECT 语句中没有结果时,我想要某个值(55) . 我试过了,但没用。 encode($arr); ?> 最佳答案 您收到错误,请尝试echo mysql_error();
我正在做报告,需要将逗号分隔字符串的列表传递到列。这很简单,我只需使用 mysql IN 运算符即可。唯一的问题是 post 变量有时可能为 NULL 那么我该如何组合: where column
我有一个包含整数值的表,我需要按列的总和进行查询: id, A, B, C 1, 21, 32, null 2, 9, 0, 124 3, null, null, 6
我在 table1 中有列,其中名称列有很多行为空 table1.name=table2.userID 所以我试图将 IFNULL 条件设置为这样 select (IFNULL(name,'empty
我有两个表,我为它们创建了左连接查询。假设它们是表A和表B。 当我触发查询时,如果表 B 包含表 A 中存在的键,则记录将根据它显示。如果键不匹配,则在所有列上显示 null 如果我必须替换 null
我将在我的选择查询中使用 IFNULL 检查 我想这样执行,当sum为null时打印0 这是我的整个查询 SELECT IFNULL(SELECT SUM(TOT_SALES_PRICE) F
可能是一个奇怪的组合,但我有一个查询,其中包括: SELECT IFNULL(`active`.`num`, `generic`.`num`) AS `num` ... `active` 和 `gen
我正在尝试使用以下方法获取剩余数量的书籍 stock = total+(receive-Issued); 表book_qnt - 图书数量列表 id | book | qnt ====
我有两个可以为 null 的 DATE 字段(PublishFrom、PublishTo)。我正在尝试通过此查询获取记录: SELECT * FROM tblNews WHERE Publis
我正在尝试更新一个表并将列 set_price 设置为另一个表中的值,如果它是 null。 两条记录共享相同的 prod_id。有人可以查看我的查询并告诉我它有什么问题吗? update list_
我正在处理一个查询,其中有一列帐号,我已使用 CONCAT 语句对其进行格式化,使数字 1-11111 而不是 11111。 有一列包含先前的帐号,但是除非确实存在先前的帐号,否则此值为 NULL。为
在下面的查询中,当它为 NULL 时,我想将“Percent”值设置为 0。但是,我似乎无法弄清楚如何将 IFNULL 函数正确地包装到我的查询中。任何帮助将不胜感激! SELECT number,
我正在开发一个 Yii2 框架应用程序,在我的数据建模阶段,我决定让数据库引擎处理简单的更新并维护我的计数器表(用于为用户返回未读消息的数量和其他内容)。 我在消息表上设计了一个触发器来增加和减少用户
您好,如果数据库中的数据为 NULL,我正在使用 mysqli 替换表中的默认值。我已经在 PHPmyAdmin 上试过了,它可以工作,但在我的代码上不行:( 这是我的 SELECT 查询: $que
id category active------------------------1 1 12 3 03 3
我正在尝试使用 IFNULL 来确定在 where 子句中使用列,但我认为我做的不正确。有人有什么建议吗? SELECT * FROM bdesclookuptable WHERE LOWER('Sa
我是一名优秀的程序员,十分优秀!