Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
So it won't fit?
$dataProvider->sort = [
'defaultOrder' => ['price' => SORT_DESC, 'title' => SORT_ASC],
'attributes' => ['price', 'title']
];
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 questionAsk a Question
731 491 924 answers to any question