• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.