Tuesday, April 10, 2018

Yii2 Sorting

In Yii2 we can use Sort class for sort the data and also define multiple columns for sorting.

Yii provides multiple types of sorting like asc, desc and user can also set default sort for all attributes.

Sorting in Yii2

use yii\data\Sort;

$sorting = new Sort([
    'attributes' => [
        'name' => [
            'asc' => ['name' => SORT_ASC, 'id' => SORT_ASC],
            'desc' => ['username' => SORT_DESC],
            'default' => SORT_DESC,
        ],
    ],
]);

$user= User::find()
    ->where(['is_visible' => 1])
    ->orderBy($sorting->orders)
    ->all();

1 comment:


  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    Hire Dedicated Yii Framework developers Developers in India

    ReplyDelete

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search