views:

90

answers:

1

Hey guys, I'm starting out with CI and there's something I don't understand. I'm writing this login page and I'd like to add the users object to the session. How do I do that? The user object comes from my user model.. For a new instance I write:

$this->load->model('user_model', 'user');

but this won't work:

$this->session->set_userdata('userobject', $this->user);

Any ideas how this is done?

+1  A: 

In the user Model create a function for retrieving the user data you want to add to session:

function get_user_data($id){
    //example query
    $query = $this->db->get_where('mytable', array('id' => $id));
    //might wanna check the data more than this but...
    if ($query->num_rows() > 0){
        return $query->row_array();
    }
    else{
        return false;
    }
}

In the controller:

$this->load->model('user_model', 'user');
$user_data = $this->user->get_user_data($id);
if(!empty($user_data)){
    $this->session->set_userdata($user_data);
}
Mitchell McKenna
I didn't even think of using an array, I was thinking of an instance from my class. Thank you.
Sled