默认排序问题 [ 2.0 版本 ]
看了下SQL默认没有排序,怎么做到默认id desc?
public function actionIndex()
{
$sort = new Sort([
'attributes' => [
'id' => [
'desc' => ['id'=>SORT_DESC],
'asc' => ['id'=>SORT_ASC],
'default' => SORT_DESC,
'label' =>'#Id',
],
'hot' => [
'asc' => ['hot'=>SORT_ASC],
'desc' => ['hot'=>SORT_DESC],
'label' =>'#即时热度',
],
'add_hot' => [
'asc' => ['add_hot'=>SORT_ASC],
'desc' => ['add_hot'=>SORT_DESC],
'label' =>'#增减热度',
],
],
]);
$query = Dynamic::find();
$pages = new Pagination([
'defaultPageSize' => 10,
'totalCount' => $query->count(),
]);
$models = $query
->where(['status' => 0])
->orderBy($sort->orders)
->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('index',[
'models' => $models,
'sort' => $sort,
'pages' => $pages,
]);
}
最佳答案
-
$sort = new Sort([ 'attributes' => [ 'id' => [ 'desc' => ['id'=>SORT_DESC], 'asc' => ['id'=>SORT_ASC], 'default' => SORT_DESC, 'label' =>'#Id', ], 'hot' => [ 'asc' => ['hot'=>SORT_ASC], 'desc' => ['hot'=>SORT_DESC], 'label' =>'#即时热度', ], 'add_hot' => [ 'asc' => ['add_hot'=>SORT_ASC], 'desc' => ['add_hot'=>SORT_DESC], 'label' =>'#增减热度', ], ], 'defaultOrder' => ['id' => SORT_DESC], // 看这里看这里看这里,重要的话说三遍 ]);
其他 3 个回答
trylife Beijing
注册时间:2015-03-26
最后登录:2020-12-07
在线时长:51小时55分
最后登录:2020-12-07
在线时长:51小时55分
- 粉丝39
- 金钱1626
- 威望40
- 积分2536