php 使用 Doctrine 按多列排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11575325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Order by multiple columns with Doctrine
提问by zootropo
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)
我需要按两列对数据进行排序(当行的列号 1 具有不同的值时,按它排序;否则,按列号 2 排序)
I'm using a QueryBuilderto create the query.
我正在使用 aQueryBuilder创建查询。
If I call the orderBymethod a second time, it replaces any previously specified orderings.
如果我orderBy第二次调用该方法,它将替换任何先前指定的排序。
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.
但是我不能为第二个参数传递两个排序方向,所以当我执行这个查询时,第一列按升序排列,第二列按降序排列。我想对他们两个都使用降序。
Is there a way to do this using QueryBuilder? Do I need to use DQL?
有没有办法做到这一点QueryBuilder?我需要使用 DQL 吗?
回答by Diego Agulló
You have to add the order direction right after the column name:
您必须在列名称之后添加订单方向:
$qb->orderBy('column1 ASC, column2 DESC');
As you have noted, multiple calls to orderBydo not stack, but you can make multiple calls to addOrderBy:
正如您所指出的,多次调用orderBydo not stack,但您可以多次调用addOrderBy:
$qb->addOrderBy('column1', 'ASC')
->addOrderBy('column2', 'DESC');
回答by Anjana Silva
In Doctrine 2.x you can't pass multiple order by using doctrine 'orderBy' or 'addOrderBy' as above examples. Because, it automatically adds the 'ASC' at the end of the last column name when you left the second parameter blank, such as in the 'orderBy' function.
在 Doctrine 2.x 中,您不能使用上述示例中的学说 'orderBy' 或 'addOrderBy' 传递多个订单。因为,当您将第二个参数留空时,它会自动在最后一列名称的末尾添加 'ASC',例如在 'orderBy' 函数中。
For an example ->orderBy('a.fist_name ASC, a.last_name ASC')will output SQL something like this 'ORDER BY first_name ASC, last_name ASC ASC'. So this is SQL syntax error. Simply because default of the orderBy or addOrderBy is 'ASC'.
例如,->orderBy('a.fist_name ASC, a.last_name ASC')将输出类似于“ORDER BY first_name ASC, last_name ASC ASC”的 SQL。所以这是 SQL 语法错误。仅仅是因为 orderBy 或 addOrderBy 的默认值是“ASC”。
To add multiple order by's you need to use 'add' function. And it will be like this.
要添加多个订单,您需要使用“添加”功能。它会是这样的。
->add('orderBy','first_name ASC, last_name ASC'). This will give you the correctly formatted SQL.
->add('orderBy','first_name ASC, last_name ASC'). 这将为您提供格式正确的 SQL。
More info on add() function. https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#low-level-api
有关 add() 函数的更多信息。https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/query-builder.html#low-level-api
Hope this helps. Cheers!
希望这可以帮助。干杯!
回答by Christian Huber
you can use ->addOrderBy($sort, $order)
您可以使用 ->addOrderBy($sort, $order)
Add:Doctrine Querybuilder btw. often uses "special" modifications of the normal methods, see select-addSelect, where-andWhere-orWhere, groupBy-addgroupBy...
添加:Doctrine Querybuilder btw。经常使用正常方法的“特殊”修改,参见select-addSelect, where-andWhere-orWhere, groupBy-addgroupBy...
回答by Victor S
The comment for orderBysource code notes: Keys are field and values are the order, being either ASC or DESC.. So you can do orderBy->(['field' => Criteria::ASC]).
对于注释orderBy的源代码注释:Keys are field and values are the order, being either ASC or DESC.。所以你可以这样做orderBy->(['field' => Criteria::ASC])。

