E
E
Evgeny Kylin2017-01-30 13:58:41
Yii
Evgeny Kylin, 2017-01-30 13:58:41

How to set DESC sorting by default for the whole site in yii2?

By default, yii2 receives data from the database in ASC sorting. How to set the default sorting for the entire site, or at least for each model separately?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-01-30
@palkan_karabov

Good afternoon.
Several options for one model.
First
Second when forming $dataProvider

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

A
atis //, 2017-01-30
@atis2345

What for?
Is it hard to do this:

User::find()
->orderBy(['id' => SORT_DESC])
->all();

UPD: move it to a separate repository.
class UsersRepository
{
    public function getLastUsers()
    {
        return new ActiveDataProvider([
            'query' => User::find(),
            'sort'  => ['defaultOrder' => ['id' => SORT_DESC]]
        ]);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question