问题描述:

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,
    ]);
相关阅读:
Top