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

(This post was last modified: 05-13-2020, 01:02 PM by jreklund.)

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
#2

So The model can insert data to db without problem. Problem is retrieving it from the database. So any help would be appreciated. Is there any step i might be missing ? I want to be able to use functions like findall() first() but model returns null when i to dump result set
Reply
#3

Have you tried with a capital A?

The real name of the function are findAll.
Reply
#4

This is of course a very strange behavior, but it is a fact.

Calling a non-existent function in the model does not cause errors or exceptions, but simply returns NULL.

Does anyone know why this is done?
Reply
#5

(05-13-2020, 01:03 PM)jreklund Wrote: Have you tried with a capital A?

The real name of the function are findAll.

Good Day. Thanks for the response. Just tried it with a capital A now it returns Array ( ) with no curly brackets 
Reply
#6

So i tried this on my controller just testing if my connection is fine.

$db = \Config\Database::connect();

$query = $db->query('SELECT * FROM users');
$results = $query->getResult();

print_r($results);

Result :Array ( [0] => stdClass Object ( [id] => 37 [user_id] => 9347985374985 [first_name] => Castro [last_name] => Nemaheni [phone] => 0984509343[email] => castro@company.co.za [office] => gauteng [created_at] => 2020/05/13 04:11:38 [updated_at] => [role] => homeowner [password] => $2y$10$fsaAR9VVYjbbAftCoJYLV./jcttCsTCLz1usp.4IpezJNlVUwITPS [activation] => 0 ) )

How do i resolve this. Because am trying to use model in this new way not like in ci3. Appreciate the help
Reply
#7

Can you try a var_dump($this->db); inside a function in your model and see if it's connected.
Reply
#8

(05-14-2020, 10:41 AM)jreklund Wrote: Can you try a var_dump($this->db); inside a function in your model and see if it's connected.
Result for var_dump($this->db);


object(CodeIgniter\Database\MySQLi\Connection)#49 (40) { ["DBDriver"]=> string(6) "MySQLi" ["deleteHack"]=> bool(true) ["escapeChar"]=> string(1) "`" ["mysqli"]=> NULL ["DSN":protected]=> string(0) "" ["port":protected]=> int(3306) ["hostname":protected]=> string(9) "localhost" ["username":protected]=> string(4) "root" ["password":protected]=> string(0) "" ["database":protected]=> string(9) "apples_db" ["subdriver":protected]=> NULL ["DBPrefix":protected]=> string(0) "" ["pConnect":protected]=> bool(false) ["DBDebug":protected]=> bool(false) ["cacheOn":protected]=> bool(false) ["cacheDir":protected]=> string(0) "" ["charset":protected]=> string(4) "utf8" ["DBCollat":protected]=> string(15) "utf8_general_ci" ["swapPre":protected]=> string(0) "" ["encrypt":protected]=> bool(false) ["compress":protected]=> bool(false) ["strictOn":protected]=> bool(false) ["failover":protected]=> array(0) { } ["lastQuery":protected]=> NULL ["connID"]=> bool(false) ["resultID"]=> bool(false) ["protectIdentifiers"]=> bool(true) ["reservedIdentifiers":protected]=> array(1) { [0]=> string(1) "*" } ["likeEscapeStr"]=> string(13) " ESCAPE '%s' " ["likeEscapeChar"]=> string(1) "!" ["dataCache"]=> array(0) { } ["connectTime":protected]=> NULL ["connectDuration":protected]=> NULL ["pretend":protected]=> bool(false) ["transEnabled"]=> bool(true) ["transStrict"]=> bool(true) ["transDepth":protected]=> int(0) ["transStatus":protected]=> bool(true) ["transFailure":protected]=> bool(false) ["aliasedTables":protected]=> array(0) { } }
Reply
#9

That looks correct to me. I'm afraid I can't say why you get that result. I have successfully tested in the model itself and in the controller without any issue.

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
WordsModel extends Model
{
    protected 
$table 'words';
    protected 
$allowedFields = ['word'];

    public function 
getWords()
    {
        return 
$this->findAll();
    }

Reply
#10

Okay no problem please advice here. Am currently working on a property system and have already started the project using crud forms and ajax to post data.

I am using Model like we did in ci3 by creating get functions

function verify_user($email)
{
$db = \Config\Database::connect();
$query = $db->query("SELECT * FROM users WHERE email='$email'");
$result = $query->getResult();
return $result;
}

Would you advice me to continue on this route or i should find a fix to the get data issue?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB