'Getting error: CodeIgniter\Database\BaseResult::getResult in CodeIgniter
I am trying to use CodeIgniter 4 crud function findAll and I am getting the error Argument 1 passed to CodeIgniter\Database\BaseResult::getResult() as shown below:
Here is my model
<?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';
and this is my controller
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;
Other crude functions like update, save, delete are working well, only when i use find() or findAll() dont work in controller and in model itself
Solution 1:[1]
i've had the same problem. To solve it, you need to call the base class constructor in the model constructor tha you're using: parent:__construct();
Solution 2:[2]
Thanks everyone, i found the issue, in my model i was using the constructor,
public function __construct(){ $this->DB = \Config\Database::connect(); $this->BUILDER = $this->DB->table($this->table); }
i remove that now find and findAll are working
Solution 3:[3]
Most probably due to the constructor that you have issue in your model files or your controller files. Just check that. Use the code below. Do not use any constructor in the Model files.
Model View
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';
}
In Controller
use App\Models\UsuariosModel;
$myObject = new UsuariosModel()
$query = $myObject->findAll();
echo '<pre>';
print_r($query);
exit;
Solution 4:[4]
I had the same error using CI 4.1.9 when I had added a constructor to the CI Model. To resolve it (as pointed out by @emmmm and @heli-sangueve) I used the following
public function __construct()
{
parent::__construct();
//other constructor code here
}
I hope this helps someone else. I would have added comments to the above answers, but didn't have the reputation...
Solution 5:[5]
You should probably check where this getResult() is called. The only way to have this error is to pass explicitiy something null to getResult() method.
Probably a variable not declared properly somewhere.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Heli Sangueve |
Solution 2 | Mauro Dias |
Solution 3 | Dave |
Solution 4 | Beyond Local |
Solution 5 | Benjamin Ghenne |