如何在codeigniter 4中使用查询生成器类分页

pdkcd3nj  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(182)

我正在用CI v4.0.4做项目。我喜欢使用查询生成器,但我发现很难对数据库中的数据分页。当我运行它时,我得到错误调用未定义的方法CodeIgniter\Database\MySQLi\Result::paginate()。我如何使用查询生成器类对模型和控制器方法分页?

//my model
    public function getData($page=0,$perPage=100){
        $builder = $this->db->table('categories')->paginate(5);
        return $builder;
    }

    //my controller
        public function index()
    {
        $pager=service('pager');
        $page=(int)(($this->request->getVar('page')!==null)?$this->request->getVar('page') : 1)-1; 

        $data = [
            'results' => $this->model->getData($page),
            'pager'  => $pager
        ];
        return view('test', $data);
    }
dgiusagp

dgiusagp1#

模型库分页

public function getWithRelations($perPage=10)
{
    $pager = service('pager');
    $page = (@$_GET['page']) ? $_GET['page'] : 1;
    $this->db->connect();
    $offset = ($page-1) * $perPage;

    $builder = $this->db->table('db_tab1 as t1');
    $data = $builder
        ->where('col','val')
        ->select('t1.*,t2.name as t2_name')
        ->join('db_tab2 as t2','t2.t1 = t1.id')
        ->orderBy('t1.id', 'DESC')
        ->get($perPage,$offset)
        ->getResult();

    $total = $builder->where('col','val')->countAllResults();

    return [
        'data'=>$data,
        'links' => $pager->makeLinks($page,$perPage,$total)
    ];
}
9njqaruj

9njqaruj2#

**//model**

public function readLists(array $data)
{
    extract($data); //search, column, order, perpage, offsett
    $builder = $this->db->table('tbl_name');
    if(isset($search))
        $builder->like('column_name', $search);
    if(isset($column))
        $builder->orderBy($column, $order);

    $rows    = $builder->get($perpage,$offset)
                       ->getResult();
    $total   = $builder->countAllResults();

    $op    =  new \stdClass;
    $op->rows  = $rows;          
    $op->total = $total;
    return $op;
}

//控制器

public function read()
    {
        $pager   = service('pager');
        $search  = (string)$this->request->getPost('search') ? $this->request->getPost('search') : '';
        $page    = (int)$this->request->getPost('page') ? $this->request->getPost('page') :  1;
        $perpage = (int)$this->request->getPost('perpage')? $this->request->getPost('perpage') : 10;
        $column  = $this->request->getPost('column') ? $this->request->getPost('column') : '';
        $order   = $this->request->getPost('order') ? 'asc' : 'desc';
        // $offset  = ($page == 1) ? 1 : ($page * $perpage) - $perpage;
        $offset  = ($page-1) * $perpage;
        $dataArr = [
            'search'  => $search,
            'column'  => $column,
            'order'   => $order,
            'perpage' => $perpage,
            'offset'  => $offset
        ];
        $query      = $this->model->readLists($dataArr);
        $results    = $query->rows;
        $total      = $query->total;
        $pagination = $pager->makeLinks($page, $perpage, $total,'pagination',0,'default');
        return $this->response->setJSON([
              'csrf'          => csrf_hash(),
              'results'       => $results,
              'rows'          => $total,
              'order'         => $order,
              'pagination'    => $pagination,
        ]);
    }

相关问题