Sarvar Nishonboyev Sarvar Nishonboyev - 8 days ago 5
PHP Question

yii2 data provider default sorting

In yii this code works for default sorting:

$dataProvider = new CActiveDataProvider('article',array(
'sort'=>array(
'defaultOrder'=>'id DESC',
),


How default sorting can be set in yii2?

tried below code, but no result:

$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder'=>'topic_order asc']
]);

Answer

I think there's proper solution

 $dataProvider = new ActiveDataProvider([
     'query' => $query,
     'sort'=> ['defaultOrder' => ['topic_order'=>SORT_ASC]]
 ]);

Official doc link