doutan1857 2015-04-28 13:36
浏览 210
已采纳

Yii2 - 用分页查询

I have a Category model that has a relation with the Products model. I am doing the following query:

$model = Category::find()->where(['slug' => $slug])->with('products')->one();

And in the view I can retrieve all the products that belong to that Category with:

foreach ($model->products as $value)...

Now I want to do the pagination in the products but I can not find how to do it in the "with" part of the query.

  • 写回答

1条回答 默认 最新

  • dqssst0144 2015-04-29 07:45
    关注

    Try this

    In Controller:

    ...
    $pages = new Pagination(['totalCount' => $model->products->count()]);
    return $this->render('index', [
             'model' => $model,
             'pages' => $pages,
        ]);
    

    In View:

    foreach ($model->products as $value) {
        // display $model here
    }
    
    // display pagination
    echo LinkPager::widget([
        'pagination' => $pages,
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?