How to access model to query database from a helper function? How to access model to query database from a helper function? codeigniter codeigniter

How to access model to query database from a helper function?


You will not be able to use $this to access your CI instance in a helper, but CI has a way to do this -

First, you need a reference to your CI instance:

$CI =& get_instance();

Then, if you need to load the model you can do it like so:

$CI->load->model('model_name');

And finally call any function in your model like this:

$CI->model_name->function_name();

Basically, use $CI instead of $this. Hope that helps.


It's not that much code repetition. I'd suggest this approach:

function __construct(){    parent::Controller();    if ($this->auth->is_logged_in() !== TRUE) // "auth" is the authentication library    {        redirect('login');    }}