【问题标题】:Yii2 - Query with PaginationYii2 - 分页查询
【发布时间】:2015-07-07 09:41:45
【问题描述】:

我有一个与 Products 模型相关的 Category 模型。 我正在执行以下查询:

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

在视图中,我可以检索属于该类别的所有产品:

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

现在我想在产品中进行分页,但在查询的“with”部分找不到如何进行。

【问题讨论】:

  • 为什么不用dataProvider?
  • 你可以使用 ActiveDataProvider 或者 ArrayDataProvider 然后使用 pagination 属性。

标签: php mysql pagination yii2


【解决方案1】:

试试这个

在控制器中:

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

在视图中:

foreach ($model->products as $value) {
    // display $model here
}

// display pagination
echo LinkPager::widget([
    'pagination' => $pages,
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2012-05-24
    • 2011-08-07
    相关资源
    最近更新 更多