[英]Yii2 Pagination is not working with orderBy
I'm trying to use Yii2 pagination with orderBy but it's not working properly. 我正在尝试将Yii2分页与orderBy一起使用,但无法正常工作。
Here's my code: 这是我的代码:
$query = Blog::find()->where(['status' => 1])->orderBy('blog_id DESC');
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$pages->pageSize = 12;
$pages->page = $_GET['page'];
$results = array();
$models = $query->offset($pages->offset)
->limit($pages->limit)
//->orderBy('blog_id DESC')
->all();
I tried a couple of scenarios but I'm still getting wrong results. 我尝试了几种情况,但仍然得到错误的结果。
Note: Pagination is working fine without the orderBy but I need it to display the newest items first. 注意:没有orderBy的分页效果很好,但是我需要它首先显示最新的项目。
Try this 尝试这个
$dataProvider = new ActiveDataProvider([
'query' => Users::find()->andWhere('UserId > :uid',[':uid' => 1])->orderBy('UserId DESC'),
'pagination' => array('pageSize' => 5),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.