如何修复我的yii2项目中的“调用数组上的成员函数offset()”错误

plupiseo  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(124)

我想在我的博客页面中设置分页。在分页之前,此操作有效。

$data = [];
    $query = Blog::find()->where(['status'=>1])->multilingual()->orderBy(['id'=>SORT_DESC])->all();

   $count = count($query);

    $pagination = new Pagination(['totalCount' => $count]);

    // limit the query using the pagination and retrieve the articles
    $data['blog'] = $query->offset($pagination->offset)->limit($pagination->limit)->all();

    return $this->render('blog-list',['data'=>$data, 'pagination'=>$pagination]);

每当我设置PageSize时,问题都没有解决。

ffscu2ro

ffscu2ro1#

删除all()

$query = Blog::find()->where(['status'=>1])->multilingual()->orderBy(['id'=>SORT_DESC]);

$totalCount = clone $query;
$pagination = new Pagination(['totalCount' => count($totalCount->all())]);
dgiusagp

dgiusagp2#

出现此错误是因为$query变量包含记录数组而不是查询对象。如果要修改查询,可以保留它。

$query = Blog::find()->where(['status'=>1])->multilingual();

$count = $query->count();
$pagination = new Pagination(['totalCount' => $count]);

// limit the query using the pagination and retrieve the articles
$data['blog'] = $query->orderBy(['id'=>SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all();

return $this->render('blog-list',['data'=>$data, 'pagination'=>$pagination]);

相关问题