Sunday, January 2, 2022

[FIXED] Yii2 data provider default sorting

Issue

In Yii 1.1 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']
]);

Solution

I think there's proper solution. Configure the yii\data\Sort object:

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

Official doc link



Answered By - Alex

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.