S
S
Sergey Desh2019-07-20 14:06:20
Yii
Sergey Desh, 2019-07-20 14:06:20

How to sort the selection by status?

There are post statuses: 1. New, 2. Expired, 3. In Progress, 4. Resolved, 5. Archived.
Each entry has only one of the listed statuses.
It is necessary to withdraw the expired ones first, then the new ones, and then all the rest.
As I understand it, you need to

SELECT * FROM TABLE ORDER BY status_id=2 DESC, status_id=1 DESC, status_id ASC;

I'm trying to fulfill
$dataProvider->query->orderBy(['status_id' => [2, 1]]);

But there is no effect. Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-07-20
@serdesh

Sorting is configured via the sort property date of the provider

$dataProvider = new ActiveDataProvider([
     'query' => $query,
     'sort'=> [
     'attributes' => [
            'age',
            'name' => [
                'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Name',
            ],
        ],
 ]);

Fill the attributes array with the parameters by which you need to sort, and then pass these parameters through the get query string in the form ...?sort=-name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question