Welcome Guest, Not a member yet? Register   Sign In
Model Returns Null on FindAll
#11

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/model...l-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/datab...y-bindings
Reply
#12

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.
Reply
#13

(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) { }
Reply
#14

(This post was last modified: 04-04-2021, 01:22 AM by ikesela.)

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) { }
Reply
#15

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

[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]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB