Showing posts with label Yii2 sorting order. Show all posts
Showing posts with label Yii2 sorting order. Show all posts

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();

Recent Update

yii2 session handling

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

Most Search