当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Yii2 Pagination is not working with orderBy

问题描述:

I'm trying to use Yii2 pagination with orderBy but it's not working properly.

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.

网友答案:

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,
    ]);
分享给朋友:
您可能感兴趣的文章:
随机阅读: