尝试在Codeigniter 4中启动分页时返回错误



我正试图根据文档启动Codeigniter 4中的分页功能。

$model = new AppModelsUserModel();
$data = [
'users' => $model->paginate(10),
'pager' => $model->pager
];

我的控制器代码如下:

public function jobmarket() {
$this->jobs = new AppModelsJobs();
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 CodeIgniterDatabaseBaseResult::getResult() must be of the type string, null given, called in xxxx/app/vendor/codeigniter4/framework/system/Model.php on line 447

这是我的型号

public function all_jobs() {
$this->categories = new AppModelsCategories();
$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;
}
}

如有任何帮助,我们将不胜感激。

我不知道这个错误到底是从哪里来的,但我在代码中发现了一些错误。试着修复这些错误,也许它对你有帮助。以下是错误:

  1. paginate()方法返回结果,因此它必须是链中的最后一个。示例:$this->jobs->all_jobs()->paginate(20)
  2. 你可以得到这样的Pager$this->jobs->pager
  3. 如果要将all_jobs()方法与paginate()方法一起使用,则必须在all_jobs()方法中返回一个模型

以下是控制器的正确代码:

public function jobmarket() {
$this->jobs = new AppModelsJobs();
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 AppModelsCategories();
$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错误页面。试着从回溯中复制所有数据并放在这里,这有助于了解到底发生了什么。

在我的情况下,我必须用自己的findAll()方法覆盖findAll(),并检查我的特定模型是否扩展了CodeIgniter的Model类。

我基本上得到了和你一样的错误@kanarifugl。

所以我有这样的东西:

<?php namespace AppModels;
use CodeIgniterModel;
class BlogModel extends Model
{
protected $db;
public function __construct() 
{
parent::__construct();
$this->db = ConfigDatabase::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 AppControllers;
class Blog extends BaseController
{
private $blogModel;
public function __construct() 
{
$this->blogModel = new AppModelsBlogModel();
}
public function index()
{
$data = [
'section' => 'blog',
'articles' => $this->blogModel->paginate(12, 'blog'),
'pager' => $this->blogModel->pager
];
echo view('articules', $data);
}
}

最后,在我看来,使用寻呼机的部分是这样的:

<?= $pager->links('blog') ?>

我希望这能帮助到别人。

从Model类中删除构造函数以解决问题

> Argument 1 passed to CodeIgniterDatabaseBaseResult::getResult()
> pagination

相关内容

  • 没有找到相关文章

最新更新