用Doctrine命令多列 [英] Order by multiple columns with Doctrine
问题描述
我需要按照两列的顺序排列(当列的列数不同时,行按顺序排列,否则按列号2排列)
I need to order data by two columns (when the rows have different values for column number 1, order by it; otherwise, order by column number 2)
我正在使用 QueryBuilder
来创建查询。
I'm using a QueryBuilder
to create the query.
如果我调用 orderBy
If I call the orderBy
method a second time, it replaces any previously specified orderings.
我可以通过两列作为第一个参数:
I can pass two columns as the first parameter:
->orderBy('r.firstColumn, r.secondColumn', 'DESC');
但是我不能通过第二个参数的两个排序方向,所以当我执行这个查询时,第一列按升序排列,第二个下降。我想使用下拉列表。
But I cannot pass two ordering directions for the second parameter, so when I execute this query the first column is ordered in an ascending direction and the second one, descending. I would like to use descending for both of them.
有没有办法使用 QueryBuilder
?我需要使用DQL吗?
Is there a way to do this using QueryBuilder
? Do I need to use DQL?
推荐答案
您必须在列名后添加订单方向:
You have to add the order direction right after the column name:
$qb->orderBy('column1 ASC, column2 DESC');
如您所说,多次调用 orderBy
不要堆栈,但可以多次调用 addOrderBy
:
As you have noted, multiple calls to orderBy
do not stack, but you can make multiple calls to addOrderBy
:
$qb->addOrderBy('column1', 'ASC')
->addOrderBy('column2', 'DESC');
这篇关于用Doctrine命令多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!