Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2-带分页的查询_Php_Mysql_Pagination_Yii2 - Fatal编程技术网

Php Yii2-带分页的查询

Php Yii2-带分页的查询,php,mysql,pagination,yii2,Php,Mysql,Pagination,Yii2,我有一个与产品模型相关的类别模型。 我正在进行以下查询: $model = Category::find()->where(['slug' => $slug])->with('products')->one(); 在视图中,我可以通过以下方式检索属于该类别的所有产品: foreach ($model->products as $value)... 现在我想在产品中进行分页,但在查询的“with”部分找不到如何进行分页。试试这个 在控制器中: ... $pages

我有一个与产品模型相关的类别模型。 我正在进行以下查询:

$model = Category::find()->where(['slug' => $slug])->with('products')->one();
在视图中,我可以通过以下方式检索属于该类别的所有产品:

foreach ($model->products as $value)...
现在我想在产品中进行分页,但在查询的“with”部分找不到如何进行分页。

试试这个

在控制器中:

...
$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,
]);

为什么不使用dataProvider?可以使用ActiveDataProvider或ArrayDataProvider,然后使用分页属性。