[eluser]simshaun[/eluser]
What if you need a global variable whose value comes from a model?
Wherever the variable is set, I need to be able to pass $CI->session->userdata('user_id') to the model's method.
Example:
$cur_user = $CI->UsersModel->get_user_data($CI->session->userdata('user_id'));
I've tried from the config, but its not letting me perform $CI->session->userdata('user_id'). (I'm assuming because the session lib hasn't been loaded at that point, but I don't know.)
Edit:
I found a temporary solution.
I've created this library which is autoloaded.
It loads the UsersModel and creates a key in the config just like I want.
Now I can call $this->config->item('cur_user_level') in my controllers.
Is this effective? If not, how should I do it?
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class is auto-loaded by CI.
*/
class AuthInit {
var $CI;
function AuthInit()
{
$this->init();
}
/**
* Selectively set config items that contain the logged in user's data.
*/
function init()
{
$this->_get_ci();
$this->CI->load->model('UsersModel');
$user_level = $this->CI->UsersModel->get_user_level($this->CI->session->userdata('user_id'));
$this->CI->config->set_item('cur_user_level', $user_level);
}
function _get_ci()
{
$this->CI =& get_instance();
}
}
// end of file