A
A
alex5e2016-06-30 10:29:51
Yii
alex5e, 2016-06-30 10:29:51

How to sort records by multiple criteria in Yii2 ActiveDataProvider?

I need to put in the ActiveDataProvider records sorted by some criterion (for example, alphabetically), first with a positive product price, and then by the same criterion, but with a zero product price.
I tried to write two ActiveDataProvider, and then combine them into one, but it doesn’t work like that, and it’s wrong, apparently.

$notAvailableDataProvider = new ActiveDataProvider($dataProvider);
$notAvailableDataProvider->query->andWhere('`price` = ' . '0');
$dataProvider = new ActiveDataProvider($dataProvider, $notAvailableDataProvider);

Tell me how to solve this problem? It is important to ultimately get exactly the ActiveDataProvider

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2016-06-30
@karminski

So it won't fit?

$dataProvider->sort = [
                'defaultOrder' => ['price' => SORT_DESC, 'title' => SORT_ASC],
                'attributes' => ['price', 'title']
            ];

M
Maxim Timofeev, 2016-06-30
@webinar

It will not work to connect 2 ActiveDataProvider, but you can make 2 queries and combine them, and then pass them to ActiveDataProvider.
You described how you want to combine 2ActiveDataProvider, and you describe the table structure, the sampling logic, and the desired result. Because what you want is definitely possible, but what you want is definitely not.
Based on your question, Anton 's answer is quite logical and correct. Since it did not suit you, it means that we do not understand what you need and the data architecture.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question