php Yii2:如何在 Find() 的 orderby() 中添加两个字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37830594/
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
Yii2: How to add two fields in orderby() of Find()
提问by Kumar V
How to add more than one field to sort in find() method?
如何在 find() 方法中添加多个字段进行排序?
I have tried as below
我试过如下
$model::find()->orderBy([['id_date' => SORT_DESC],['item_no'=>SORT_ASC]);
But it is throwing error with query.
Orderby Query produced by yii2 is: ORDER BY 0, 1
但它在查询时抛出错误。yii2 产生的 Orderby 查询是:ORDER BY 0, 1
回答by Jurik
According to the documentation:
根据文档:
$model::find()->orderBy([
'id_date' => SORT_DESC,
'item_no'=>SORT_ASC
]);
回答by Gullu Mutullu
You have a syntax error in the following code:
您在以下代码中有语法错误:
$model::find()->orderBy([['id_date' => SORT_DESC], ['item_no' => SORT_ASC]);
The correct way of doing this is:
正确的做法是:
$model::find()->orderBy(['id_date' => SORT_DESC, 'item_no' => SORT_ASC]);
回答by kozimjon yoqubov
class NewsController extends Controller
{
public function actionIndex ()
{ $news = \common\models\News::find()->orderBy(['date' => SORT_DESC])->all();
return $this->render("index",[
'news' => $news
]);
}
}