Search code examples
phpcodeigniteroopcodeigniter-2

Codeigniter Query result returning Custom result object with setters


How exactly does CI custome object works ?

As per CI documentation You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)

$query = $this->db->query("SELECT * FROM users;");

foreach ($query->result('User') as $row)
{
   echo $row->name; // call attributes
   echo $row->reverse_name(); // or methods defined on the 'User' class
}
}

This is a very nice feature yet what Ci does is it will return an array of User objects and set attributes from row to it.

i have a problem with it that i want to have more control on what attributes to be publicly accessed and what to be modified before setting/getting.

how can i accomplish this ? can i tell CI to pass all attributes to constructor so that class can populate its own data ?

example class User

class User{
    private $data=array();
    protected $CI;
    //public $id,$name,$dob,$gender,$role,$username,$password,$salt,$picture,$lastactive;
    function __construct($data=null)
    {
        $this->data = $data; // i want to save data to a private var and allow attr. throu getters only
    }
    function set_password($p){
      $this->generateSalt();
      $this->data->password = $p.$this->data->salt;
    }
}

In a nutshell::

I want to use custom_result_object but i dont want codeigniter to populate class attributes for me, instead i want the class to receive those attrs and populate it him self the way he this its appropriate.


Solution

  • I found your question while looking for a solution for myself.

    After digging a bit in the documentation I managed to figure it out:

    class user_item {
      // you can declare all the attributes you want as private
      private $id,$name,$dob,$gender,$role,$username,$password,$salt,$picture,$lastactive;
    
      function __construct(){
        // you can use the constructor to format data as needed
        $this->username = strtouppper($this->username);  
      }
    
      public function set_password($p){
        $this->generateSalt();
        $this->password = $p.$this->salt;
      }
    
      public function get_password(){
        return $this->password;
      }
    }
    

    Once set up, you can instantiate this class from $this->db->result()

    class User_model extends CI_Model {
       public function get_user($id){
         return $this->db->get_where('users', array('id' => $id), 1)->result('user_item');
       }
    }
    

    And call any public method or attribute of the class as needed

    class Users extends CI_Controller {
      function __construct(){
         $this->load->model('user');
      }
      public function profile($user_id){
        var $myUser = $this->user->get_user($user_id);
        $myUser->set_password('myPassword');
        echo $myUser->get_password();
      }
    }
    

    I have simplified the code to make it clearer, but you get the idea.