【问题标题】:Getting error: CodeIgniter\Database\BaseResult::getResult in CodeIgniter出现错误:CodeIgniter 中的 CodeIgniter\Database\BaseResult::getResult
【发布时间】:2020-07-19 08:38:52
【问题描述】:

我正在尝试使用 CodeIgniter 4 crud 函数 findAll,我收到错误参数 1 传递给 CodeIgniter\Database\BaseResult::getResult(),如下所示:

这是我的模型

<?php 
namespace App\Models\Admin\User_management;
use CodeIgniter\Model;

class UsuariosModel extends Model
{
    
    protected $table = 'users';
    protected $primaryKey = 'user_id';
    
    protected $returnType = 'array';

    protected $allowedFields = ['user_login', 'user_psw', 'user_full_name', 'user_email', 'user_telef', 'user_image', 'user_date_of_birth', 'user_gender', "user_created_at", "user_updated_at", "user_deleted_at"];

    protected $useTimestamps = true;
    protected $useSoftDeletes = true;

    protected $createdField  = 'user_created_at';
    protected $updatedField  = 'user_updated_at';
    protected $deletedField  = 'user_deleted_at';
    
    
    

这是我的控制器

 public function delete_users(){
        //$this->validateViewUserData();
        //$user_id = $this->request->getVar('user_id', FILTER_SANITIZE_STRING);
        //$this->deleteUsers($user_id);
        
        
        $user_model = new UsuariosModel();
        
        $users = $user_model->findAll();
    }

DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_login` varchar(40) NOT NULL,
  `user_psw` varchar(255) NOT NULL,
  `user_full_name` varchar(100) NOT NULL,
  `user_email` varchar(80) NOT NULL,
  `user_telef` int(16) NOT NULL,
  `user_image` varchar(255) DEFAULT NULL,
  `user_created_at` datetime NOT NULL,
  `user_updated_at` datetime DEFAULT NULL,
  `user_deleted_at` datetime DEFAULT NULL,
  `user_date_of_birth` date NOT NULL,
  `user_gender` int(1) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=119 DEFAULT CHARSET=utf8;

更新、保存、删除等其他粗略功能运行良好,只有当我使用 find() 或 findAll() 时,控制器和模型本身不起作用

【问题讨论】:

  • 看起来您可能使用的是旧版本的 CI 4.0.2?您是否尝试过使用最新版本?
  • @TimBrownlaw 我用的是 4.0.2 但现在我用的是 4.0.4 问题还是一样,所以我不知道发生了什么
  • 好吧,我已经获取了您的代码,根据您的设置创建了一个测试模式,但我无法在 PHP 7.4.8 上使用 CI 4.04 重新创建您的错误 - (Kubuntu 18.04)。您能否提供一个示例数据库架构。
  • 只需仔细检查您的行号是否已更改错误,以确保您确实安装了新系统......在这里抓住稻草但值得检查。
  • 不,仍然没有错误。我从我的虚拟插入中得到一个结果。执行 getLastQuery() 来检查 SQL,我得到 - SELECT * FROM users WHERE users.user_deleted_at IS NULL。所以不确定你的最终有什么不同。您是否尝试过重新安装系统文件夹,即使您已升级到 CI 4.04。我就是无法打破它。

标签: php codeigniter


【解决方案1】:

我也遇到了同样的问题。 要解决它,您需要调用 您正在使用的模型构造函数中的基类构造函数: 父:__construct();

【讨论】:

    【解决方案2】:

    谢谢大家,我发现了问题,在我的模型中我使用了构造函数, public function __construct(){ $this-&gt;DB = \Config\Database::connect(); $this-&gt;BUILDER = $this-&gt;DB-&gt;table($this-&gt;table); } 我删除了现在 find 和 findAll 正在工作

    【讨论】:

    • 你可以添加“parent::__construct();”在构造函数的开头,然后继续使用构造函数代码。
    【解决方案3】:

    很可能是由于您的模型文件或控制器文件中的构造函数存在问题。只是检查一下。使用下面的代码。不要在模型文件中使用任何构造函数。

    模型视图

    class UsuariosModel extends Model
    {
        
        protected $table = 'users';
        protected $primaryKey = 'user_id';
        
        protected $returnType = 'array';
    
        protected $allowedFields = ['user_login', 'user_psw', 'user_full_name', 'user_email', 'user_telef', 'user_image', 'user_date_of_birth', 'user_gender', "user_created_at", "user_updated_at", "user_deleted_at"];
    
        protected $useTimestamps = true;
        protected $useSoftDeletes = true;
    
        protected $createdField  = 'user_created_at';
        protected $updatedField  = 'user_updated_at';
        protected $deletedField  = 'user_deleted_at';
    }
    

    在控制器中

    use App\Models\UsuariosModel;
    
    $myObject = new UsuariosModel()
    $query  = $myObject->findAll();
    
    echo '<pre>';
    print_r($query);
    exit;
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案4】:

    您可能应该检查调用此 getResult() 的位置。出现此错误的唯一方法是将 null 显式传递给 getResult() 方法。

    可能是某个变量没有正确声明。

    【讨论】:

    • 被这个 $users = $user_model->findAll();在我的控制器中,函数 delete_users() 的代码
    猜你喜欢
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2015-07-15
    • 1970-01-01
    • 2017-09-03
    • 2015-10-24
    相关资源
    最近更新 更多