如何修复我的 yii2 项目中的"Call to a member function offset() on array"错误



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

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

每当我设置页面大小时,问题都没有解决。

删除all()

$query = Blog::find()->where(['status'=>1])->multilingual()->orderBy(['id'=>SORT_DESC]);
$totalCount = clone $query;
$pagination = new Pagination(['totalCount' => count($totalCount->all())]);

您遇到错误$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]);

相关内容

  • 没有找到相关文章

最新更新