CodeIgniter Forums
Model Returns Null on FindAll - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Model Returns Null on FindAll (/showthread.php?tid=76442)

Pages: 1 2


RE: Model Returns Null on FindAll - jreklund - 05-15-2020

Hi, sorry I could not find a solution, but it looks like you did everything correctly.

If you don't extend the Model you should load the database like this:
https://codeigniter.com/user_guide/models/model.html#manual-model-creation

Also you are a subject to SQL injection, that can access all your data (or delete it) with that SQL query.
You should use query bindings:
https://codeigniter.com/user_guide/database/queries.html#query-bindings


RE: Model Returns Null on FindAll - remesses_thegreat - 05-27-2020

Good Day

Thanks for the advice. Been using as advised worked fine.

I will update if i find the problem.

Just another quick question

When using Curl Request to consume Soap Services how do i get function within the body. As the body returns a list of functions that i should be able to invoke but I'm new to curl was using SoapClient on CI3.


RE: Model Returns Null on FindAll - arios - 04-03-2021

(05-12-2020, 09:16 PM)I was facing this same problem just recently, the problem I had was the database, you need to set deleted_at (if using) to default NULL, because find and findAll query add WHERE deleted_at IS NULL, so, that solved my problemremesses_thegreat Wrote: Good Day Everyone. Please urgently help with My Model. I want to get users from table users in db. Model returns Null/ array(0). Below is my code for the controller and model. Thanks in advance 


UserModel

PHP Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $DBGroup = 'default';
    protected $table      = 'users';
    protected $primaryKey = 'id';

    protected $returnType     = 'array';
    protected $useSoftDeletes = true;

    protected $allowedFields = ['user_id', 'first_name','last_name','phone','email','office','created_at','updated_at','role','password'];
    
    protected $beforeInsert = ['beforeInsert'];
    protected $beforeUpdate = ['beforeUpdate'];

  
    protected function beforeInsert(array $data){
        $data = $this->passwordHash($data);
        $data['data']['created_at'] = date('Y/m/d H:i:s');
        $data['data']['updated_at'] = date('Y/m/d H:i:s');

        return $data;
    }

    protected function beforeUpdate(array $data){
        $data = $this->passwordHash($data);
        $data['data']['updated_at'] = date('Y/m/d H:i:s');
        return $data;
    }

    protected function passwordHash(array $data){
        if(isset($data['data']['password']))
            $data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
            return $data;
   
    }

    



Controller 

PHP Code:
public function get_users()
    {
       $userModel = new \App\Models\UserModel();

       $user = $userModel->findall();

       print_r($user);
    

Result 

array(0) { }



RE: Model Returns Null on FindAll - ikesela - 04-04-2021

Turn of softdelete if dont use deleted_at

Code:
$useSoftDeletes = false


(05-12-2020, 09:16 PM)I was facing this same problem just recently, the problem I had was the database, you need to set deleted_at (if using) to default NULL, because find and findAll query add WHERE deleted_at IS NULL, so, that solved my problemremesses_thegreat Wrote: Good Day Everyone. Please urgently help with My Model. I want to get users from table users in db. Model returns Null/ array(0). Below is my code for the controller and model. Thanks in advance 


UserModel

PHP Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $DBGroup = 'default';
    protected $table      = 'users';
    protected $primaryKey = 'id';

    protected $returnType     = 'array';
    protected $useSoftDeletes = true;

    protected $allowedFields = ['user_id', 'first_name','last_name','phone','email','office','created_at','updated_at','role','password'];
    
    protected $beforeInsert = ['beforeInsert'];
    protected $beforeUpdate = ['beforeUpdate'];

  
    protected function beforeInsert(array $data){
        $data = $this->passwordHash($data);
        $data['data']['created_at'] = date('Y/m/d H:i:s');
        $data['data']['updated_at'] = date('Y/m/d H:i:s');

        return $data;
    }

    protected function beforeUpdate(array $data){
        $data = $this->passwordHash($data);
        $data['data']['updated_at'] = date('Y/m/d H:i:s');
        return $data;
    }

    protected function passwordHash(array $data){
        if(isset($data['data']['password']))
            $data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
            return $data;
   
    }

    



Controller 

PHP Code:
public function get_users()
    {
       $userModel = new \App\Models\UserModel();

       $user = $userModel->findall();

       print_r($user);
    

Result 

array(0) { }



RE: Model Returns Null on FindAll - devprecious - 06-28-2021

The reason why is because of the protected $useSoftDeletes = true; comment it or set it to false;


RE: Model Returns Null on FindAll - Matmanuel - 08-15-2023

[i]If you have protected $useSoftDeletes=true;I just faced the same issue until I realized that my database at the deleted_at field does not accept null values, so I had to set 'deleted_at timestamp null default null' in my migrations file so that the database accepts null values at the deleted_at field, which allows me to retrieve all records with deleted_at=null in the database when using find, where, or findAll queries.[/i]