出现错误:CodeIgniter\Database\BaseResult::getResult

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


https://img1.mukewang.com/64d5f1c00001f26112430568.jpg

这是我的模型


<?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() 时,它们在控制器和模型本身中不起作用


PIPIONE
浏览 176回答 6
6回答

慕的地8271018

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

达令说

<?php&nbsp;namespace App\Models\Admin\User_management;use CodeIgniter\Model;class UsuariosModel extends Model{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; protected $table = 'users';&nbsp; &nbsp; protected $primaryKey = 'user_id';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; protected $returnType = 'array';&nbsp; &nbsp; 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"];&nbsp; &nbsp; protected $useTimestamps = true;&nbsp; &nbsp; protected $useSoftDeletes = true;&nbsp; &nbsp; protected $createdField&nbsp; = 'user_created_at';&nbsp; &nbsp; protected $updatedField&nbsp; = 'user_updated_at';&nbsp; &nbsp; protected $deletedField&nbsp; = 'user_deleted_at';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;public function __construct(){&nbsp;parent::__construct();&nbsp;$db = \Config\Database::connect();&nbsp;$this->builder = $db->table('NAME OF THE TABLE');}&nbsp; &nbsp;

元芳怎么了

Codeigniter 4 在模型上预定义了变量,但其中很少有只需要true或fals,您只需要小心使用它们即可:这些是以下定义的变量,其protected范围为model:protected $table = '';protected $primaryKey = 'id';protected $useAutoIncrement = true;protected $returnType = 'array';protected $useSoftDeletes = true;protected $allowedFields = [];protected $useTimestamps = false;protected $createdField = 'created_at';protected $updatedField = 'updated_at';protected $deletedField = 'deleted_at';protected $validationRules = [];protected $validationMessages = [];protected $skipValidation = false;试试这个:change $useSoftDeletes value from `true` to `false`&nbsp;&nbsp; &nbsp;&nbsp;并运行,如果它解决了您的问题,则无需执行任何其他操作。如果这不能帮助解决问题,则注释这些所有变量,仅取消注释,$table然后$allowedFields刷新页面(如果它解决了问题),然后一一检查其余boolean情况true,或者false可能是其中一个真正的问题。就我而言,我遇到了这个问题。Note: 另请记住一件事,如果您使用构造函数方法controller或model然后必须调用parent::__construct();其他方法,则会出现此错误:>&nbsp; &nbsp; &nbsp;TypeError>&nbsp; &nbsp; &nbsp;CodeIgniter\Database\BaseResult::getResult(): Argument #1 ($type) must be of type string, null given, called in

Cats萌萌

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

森栏

使用了以下方法public function __construct() {    parent::__construct();    //other constructor code here}我希望这对其他人有帮助。

慕侠2389804

最有可能是由于模型文件或控制器文件中的构造函数存在问题。只要检查一下即可。使用下面的代码。不要在模型文件中使用任何构造函数。模型视图class UsuariosModel extends Model{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; protected $table = 'users';&nbsp; &nbsp; protected $primaryKey = 'user_id';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; protected $returnType = 'array';&nbsp; &nbsp; 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"];&nbsp; &nbsp; protected $useTimestamps = true;&nbsp; &nbsp; protected $useSoftDeletes = true;&nbsp; &nbsp; protected $createdField&nbsp; = 'user_created_at';&nbsp; &nbsp; protected $updatedField&nbsp; = 'user_updated_at';&nbsp; &nbsp; protected $deletedField&nbsp; = 'user_deleted_at';}在控制器中use App\Models\UsuariosModel;$myObject = new UsuariosModel()$query&nbsp; = $myObject->findAll();echo '<pre>';print_r($query);exit;
打开App,查看更多内容
随时随地看视频慕课网APP