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,
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000