我有一个与产品模型相关的类别模型。 我正在执行以下查询:
$model = Category::find()->where(['slug' => $slug])->with('products')->one();
在 View 中,我可以通过以下方式检索属于该类别的所有产品:
foreach ($model->products as $value)...
现在我想在产品中进行分页,但在查询的“with”部分找不到如何进行分页。
最佳答案
试试这个
在 Controller 中:
...
$pages = new Pagination(['totalCount' => $model->products->count()]);
return $this->render('index', [
'model' => $model,
'pages' => $pages,
]);
在 View 中:
foreach ($model->products as $value) {
// display $model here
}
// display pagination
echo LinkPager::widget([
'pagination' => $pages,
]);
关于php - Yii2 - 分页查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921117/