尝试在 Codeigniter 4 中启动分页时返回错误
Posted
技术标签:
【中文标题】尝试在 Codeigniter 4 中启动分页时返回错误【英文标题】:Error returned when trying to initiate pagination in Codeigniter 4 【发布时间】:2020-07-25 00:13:03 【问题描述】:我正在尝试按照documentation 在 Codeigniter 4 中启动分页功能。
$model = new \App\Models\UserModel();
$data = [
'users' => $model->paginate(10),
'pager' => $model->pager
];
我的控制器代码如下:
public function jobmarket()
$this->jobs = new \App\Models\Jobs();
if (!$this->ionAuth->loggedIn())
return redirect()->to('/logg-inn');
echo view("dashboard/header", ([
'ionAuth' => $this->ionAuth,
'uri' => $this->uri,
]));
echo view("dashboard/jobmarket", ([
'session' => $this->session,
'ionAuth' => $this->ionAuth,
'validation' => $this->validator,
'jobs' => $this->jobs->paginate(20)->all_jobs(),
'pager' => $this->jobs->pager()->all_jobs(),
]));
echo view("assets/footer");
但是,运行此程序时,我收到以下错误:
Argument 1 passed to CodeIgniter\Database\BaseResult::getResult() must be of the type string, null given, called in xxxx/app/vendor/codeigniter4/framework/system/Model.php on line 44
7
这是我的模特
public function all_jobs()
$this->categories = new \App\Models\Categories();
$builder = $this->db->table('jobs');
$builder->select('*');
$builder->join('users', 'users.id = jobs.jobs_u_id');
$builder->join('categories', 'category_id = jobs.jobs_category');
// Make sure to not show current user's listings, since these will show up under "My listings"
$builder->where('jobs_u_id !=', $this->current_user->id);
// Check that the listing reflects users chosen categories
$builder->whereIn('category_id', $this->categories->user_categories());
$builder->orderBy('jobs_id', 'desc');
$query = $builder->get();
if ($builder->countAllResults() > 0)
return $query->getResult();
else
return false;
任何解决此问题的帮助将不胜感激。
【问题讨论】:
可以不分页试试吗?似乎错误是关于模型而不是分页本身。此外,您的寻呼机实现可能不正确。我尝试了 $model->pager,但它对我不起作用,所以我将其更改为 $inventoryModel->pager->getDetails()。尝试将 $this->jobs->pager()->all_jobs() 更改为 $this->jobs->pager->getDetails() 看看是否可行。 另外,从代码中检出寻呼机类。它会给你一些想法。但很可能我认为您的错误与模型本身有关。 $builder->join('categories', 'category_id = jobs.jobs_category');不应该是 category.id 或 categories.category_id。我怀疑 category_id 来自用户表。注释掉连接并一一添加。查看sql是否正确 print_r($this->db->last_query()); print_r($query) 查看结果。在 phpmyadmin 或 mysql developer 中运行查询以查看返回的内容。 也分享你的分类模型 【参考方案1】:我不知道您究竟是从哪里得到这个错误的,但我在您的代码中发现了一些错误。尝试修复这些错误,也许它可以帮助你。以下是错误:
paginate()
方法返回结果,因此它必须是链中的最后一个。
示例:$this->jobs->all_jobs()->paginate(20)
您可以像这样获得Pager
:$this->jobs->pager
如果要将 all_jobs()
方法与 paginate()
方法一起使用,则必须在 all_jobs()
方法中返回模型
这是您的控制器的正确代码:
public function jobmarket()
$this->jobs = new \App\Models\Jobs();
if (!$this->ionAuth->loggedIn())
return redirect()->to('/logg-inn');
echo view("dashboard/header", ([
'ionAuth' => $this->ionAuth,
'uri' => $this->uri,
]));
echo view("dashboard/jobmarket", ([
'session' => $this->session,
'ionAuth' => $this->ionAuth,
'validation' => $this->validator,
'jobs' => $this->jobs->all_jobs()->paginate(20),
'pager' => $this->jobs->pager,
]));
echo view("assets/footer");
这是您模型的正确代码:
public function all_jobs()
$this->categories = new \App\Models\Categories();
$builder = $this->db->table('jobs');
$builder->select('*');
$builder->join('users', 'users.id = jobs.jobs_u_id');
$builder->join('categories', 'category_id = jobs.jobs_category');
// Make sure to not show current user's listings, since these will show up under "My listings"
$builder->where('jobs_u_id !=', $this->current_user->id);
// Check that the listing reflects users chosen categories
$builder->whereIn('category_id', $this->categories->user_categories());
$builder->orderBy('jobs_id', 'desc');
return $this;
要查看导致错误的确切位置,请尝试在根目录下的.env
文件中设置CI_ENVIRONMENT = development
。之后尝试重新加载您发现此错误的页面。您将看到带有回溯的 CodeIgniter 的错误页面。尝试从回溯复制所有数据并将其放置在此处,这有助于了解到底发生了什么。
【讨论】:
【参考方案2】:就我而言,我必须用自己的 findAll()
方法覆盖 findAll()
,并检查我的特定模型是否扩展了 CodeIgniter 的 Model
类。
我基本上遇到了和你@kanarifugl 一样的错误。
所以我有这样的事情:
<?php namespace App\Models;
use CodeIgniter\Model;
class BlogModel extends Model
protected $db;
public function __construct()
parent::__construct();
$this->db = \Config\Database::connect();
$this->table = 'Blog';
public function findAll(int $limit = 12, int $offset = 0)
$builder = $this->builder();
$builder->select('*');
$builder->orderBy('blog_id', 'DESC');
$builder->join('Categories', 'Categories.category_id = Blog.category_id');
if ($this->tempUseSoftDeletes === true)
$builder->where($this->table . '.' . $this->deletedField, null);
$row = $builder->limit($limit, $offset)
->get();
$row = $row->getResult($this->tempReturnType);
$eventData = $this->trigger('afterFind', ['data' => $row, 'limit' => $limit, 'offset' => $offset]);
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
return $eventData['data'];
然后在我的博客控制器中,我刚刚有了这个:
<?php namespace App\Controllers;
class Blog extends BaseController
private $blogModel;
public function __construct()
$this->blogModel = new \App\Models\BlogModel();
public function index()
$data = [
'section' => 'blog',
'articles' => $this->blogModel->paginate(12, 'blog'),
'pager' => $this->blogModel->pager
];
echo view('articules', $data);
最后,在我看来,使用寻呼机的部分是这样的:
<?= $pager->links('blog') ?>
我希望这对某人有所帮助。
【讨论】:
【参考方案3】:从Model类中去掉构造函数来解决问题
> Argument 1 passed to CodeIgniter\Database\BaseResult::getResult()
> pagination
【讨论】:
以上是关于尝试在 Codeigniter 4 中启动分页时返回错误的主要内容,如果未能解决你的问题,请参考以下文章