我正在尝试根据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;
}
}
任何解决此问题的帮助将不胜感激。
我不知道您到底从哪里得到此错误,但是我在您的代码中发现了一些错误。尝试修复这些错误,也许可以帮助您。这些错误:
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;
}
要查看导致错误的确切位置,请尝试在根目录的CI_ENVIRONMENT = development
文件中设置.env
。之后,尝试重新加载您发现此错误的页面。您将看到一个带有回溯的CodeIgniter错误页面。尝试从backtrace复制所有数据并将其放置在此处,这有助于了解到底发生了什么。