2

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.

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Where did you put the user_item class however? In libraries?
@NaturalBornCamper I will put it in the same file as User_model. So it's auto-loaded when you do $this->load->model('user').
Ok thanks! Too bad it seems they didn't put any documentation about that
0

this example controller using result array and object

if ($this->session->userdata('id_jurusan') ==1) {
            $where=array('id_jurusan'=>$this->session->userdata('id_jurusan'));
            $value = $this->session->userdata('id_jurusan');
            $value2 = $this->session->userdata('username');
            $data['rule']=$this->guru_mod->get_where($where,'forward_changing')->result();
            $data['fuzzy']=$this->guru_mod->get_data_all('fuzzy')->result();
            $data['artikel']=$this->guru_mod->get_data_all('artikel')->result();
            $data['kondisi']=$this->guru_mod->get_where($where,'kondisi')->result();
            $data['artikel2'] = $this->guru_mod->get_data_all2('artikel','id_jurusan',$value);
            $data['riwayat_rule'] = $this->guru_mod->get_data_all2('forward_changing','username',$value2);
            $data['kondisi_rule'] = $this->guru_mod->get_data_all2('kondisi','id_jurusan',$value);
            $this->load->view('guru/daftar_rule',$data);
        }

1 Comment

Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.