Doctrine 2 mysql FIELD功能按顺序排列 [英] Doctrine 2 mysql FIELD function in order by
问题描述
我试图在查询中的order by子句中使用MySQL FIELD函数。我假设Doctrine 2不支持FIELD功能开箱即用 - 是真的吗?如果是这样,我该怎么用呢?我将整个查询转换成本机查询吗?有没有添加这个功能的Doctrine 2扩展?
I'm trying to use the MySQL FIELD function in an order by clause in a query. I'm assuming that Doctrine 2 doesn't support the FIELD function out of the box - is that true? If so, how can I use it? Will I have to turn my whole query into a native query? Is there a Doctrine 2 extension that adds this functionality?
推荐答案
Jeremy Hicks,谢谢您的扩展程序。
我没有知道如何将你的函数连接到教义,但最后我找到答案。
Jeremy Hicks, thanks for your extension. I didn`t know how to connect your function to doctrine, but finally i find answer.
$doctrineConfig = $this->em->getConfiguration();
$doctrineConfig->addCustomStringFunction('FIELD', 'DoctrineExtensions\Query\Mysql\Field');
我需要 FIELD
函数来订购我的实体我选择 IN
表达式。但是您只能在 SELECT,WHERE,BETWEEN
子句中使用此功能,而不能在 ORDER BY
中。
I need FIELD
function to order my Entities that i select by IN
expression. But you can use this function only in SELECT, WHERE, BETWEEN
clause, not in ORDER BY
.
解决方案:
$qb
->select("r, field(r.id, " . implode(", ", $ids) . ") as HIDDEN field")
->from("Entities\Round", "r")
->where($qb->expr()->in("r.id", $ids))
->orderBy("field");
为避免在您的结果中添加字段
别名你需要放置 HIDDEN
关键字。所以这样可以在Doctrine 2.2中的 IN
表达式中订单值。
To avoid adding field
alias into your result row you need put HIDDEN
keyword. So this how to be able order values in IN
expression in Doctrine 2.2.
这篇关于Doctrine 2 mysql FIELD功能按顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!