带有显示错误的连接表的 Codeigniter 4 分页

Codeigniter 4 Pagination with join tables showing error

我正在从事 codeigniter 项目。我正在显示新闻 table 并加入左侧新闻类别并分页。当我尝试下面的代码时,出现错误 Call to undefined method CodeIgniter\Database\MySQLi\Builder::paginate()。如何用连接和分页显示新闻数据?

    $db      = \Config\Database::connect();
    $news_tbl = $db->table('tbl_news')->join('tbl_category', 'tbl_news.category_id = tbl_category.category_id');
    $data['news_fetched'] = $news_tbl->paginate(10);
    $data['pager'] = $news_fetched->pager;
    $data['links'] = $data['pager']->links();

$db->table() 创建一个查询生成器对象。分页适用于模型对象。您没有以正确的方式设置分页。 您应该创建一个名为 tblNewsModel 的模型文件,它将为您处理分页。

<?php
namespace App\Models;

class tblNewsModel extends \CodeIgniter\Model {

    protected $table = 'tbl_news';
    protected $primaryKey = 'your_pk_id';

    // your function to paginate
    public function paginateNews(int $nb_page) {
        return $this->select()->join('tbl_category', 'tbl_news.category_id = tbl_category.category_id')->paginate($nb_page);
    }

}

然后在你的控制器中,创建这个模型的一个新实例并让他给你分页

$tblNewsModel = new \App\Models\tblNewsModel();
$data['news_fetched'] = $tblNewsModel->paginateNews(10);
$data['pager'] = $tblNewsModel->pager;
$data['links'] = $data['pager']->links();