phpcodeigniter-4

Basic understanding of returned values for Codeigniter4


I've created my first model in CodeIgniter 4 (with Shield).

I am wondering about the many additional fields I received from the following model-function.

Here is the simple function from this model:

public function users_get_by_id(int $id, array $fields=array())
{ 
  if(empty($fields)){
    return $this->find($id);
  }
  else
  { 
    return $this->select($fields)->where('id', $id); 
  }
}

Inside the controller I get and print out the returned values like this

$data = $this->UsersModel->users_get_by_id(user_id(),['username','department_main']);
echo"<pre>";
print_r($data);

Now I am wondering why in my $data are not only the values from the tables/fields called by the "users_get_by_id" function.

In CI3 I only recieved a array with the table fields and values.

Now there is a lot more information.

Example: print_r($data);

gives this:

App\Models\UsersModel Object([pager] =>
[db:protected] => CodeIgniter\Database\MySQLi\Connection Object
([DSN:protected] =>
[port:protected] => 3306
[hostname:protected] => ....
[username:protected] => ...
[password:protected] => ...
[database:protected] =>....
[DBDriver] => MySQLi
...many more values follow...
)

By the way what does it mean by the prepended parts like [db:protected] etc?

Looking in the CodeIgniter docs brings no info about it.


Solution

  • I can't find the docs for it now but I believe you need to actually execute the query, with ->get(), or if you're only expecting 1 result, ->first(). So your Model code should be:

    return $this->select($fields)->where('id', $id)->first();
    

    Without it, you are returning the Model class, hence all the extra stuff you're seeing.