在Codeigniter 4中使用分页时报错
开发学院2022-04-12 13:34:19
我试图按照文档中的说明在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 447
这是我的Model层代码
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; } }
任何解决这个问题的帮助将不胜感激。
解决方案
我不知道你是从哪里得到这个错误的,但是我在你的代码中发现了一些错误。试着修复这些bug,也许对你有帮助。这些是错误:
paginate()方法返回结果,因此它必须是链中的最后一个,例如:$this->jobs->all_jobs()->paginate(20)
你可以得到这样一个分页器:$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错误页面。尝试从backtrace复制所有数据并放在这里,这有助于了解到底发生了什么。
相关文章
- windows修改ollama程序和模型保存位置
- UE5中使用蓝图实现对象池功能
- UE5开发2D/3D混合平台跳跃游戏优化操作体验
- UE5敌人直接放置场景ok,代码生成不执行AI
- UE5中开发HD-2D游戏的优化设置与2D角色导入技巧
- nginxSpringboot项目常见配置
- 在MacOS上部署ComfyUI的指南
- 解决UE5开发Topdown2D动作游戏的旋转问题
- UE5开发2D游戏设置排序的步骤.
- 大幅提升FPS!Unreal Engine 5 最佳 2D 设置
- Aseprite在线编译教程
- 探索Nexa AI:开源边缘智能的新纪元
- Springboot项目允许根目录txt文件被访问
- lnmp一键安装包多php环境安装
- Python虚拟环境整合包制作:一键打包与运行指南
- aws云服务器使用root登录
- nginx配置允许跨域
- nginx配置springboot反向代理,同时允许上传路径可以直接被访问
- CentOS8更换国内安装源
- 在Linux中使用FFmpeg修剪视频