Answer the question
In order to leave comments, you need to log in
Bug in Yii2 - pagination doesn't work with orderBy()?
The documentation provides a very good example of how to work with pagination
. However, as soon as you set it, the calculation starts to fail. And it is buggy in this way - on some page it will display 15, on some 14. At the same time, the pageSize parameter is set to 15. But if you remove orderBy(), then everything works perfectly.
It seems that I am not the first one who encountered this, but I could not find an adequate solution anywhere so far.orderBy('id DESC')
$query = Article::find()->where(['status' => 1])
->orderBy('id DESC');
Answer the question
In order to leave comments, you need to log in
The solution turned out to be quite trivial, you need to put groupBy() on the same column before orderBy().
$query->groupBy('items.id')
->orderBy('items.id DESC');
try adding orderBy after pagination, like this:
use yii\data\Pagination;
$query = Article::find()->where(['status' => 1]);
$count = $query->count();
$pagination = new Pagination(['totalCount' => $count]);
$articles = $query->orderBy('id DESC')->offset($pagination->offset)
->limit($pagination->limit)
->all();
If you use ActiveDataProvider, then sorting must be set in it, and not in query
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question