Welcome Guest, Not a member yet? Register   Sign In
Where to put 'helper' function that calls a model?
#1

[eluser]mattots[/eluser]
I'm fairly new to CodeIgniter and the whole MVC approach so forgive me if this is a bit elementary!

I have got a 'helper'-type function (ie. it doesn't actually load a view, just takes an argument and spits back a result) that gets used by several of my controllers. Normally I would put this in a helper file in the helpers directory as that seems the most logical thing to do. However, the function needs to run several database queries so has model calls in it.

I'm assuming that calling a model from a helper function won't work. So at the moment I've simply duplicated the function in all the controllers that require it.

I guess there must be a better way of doing this. I did consider moving the model calls into the main controller functions and passing the query results to the function as parameters, but this would mean that I was simply duplicating code in a different place.

Any help would be appreciated.
#2

[eluser]davidbehler[/eluser]
You can load a model within a helper or a library!
Code:
$CI =& get_instance();
$CI->load->model('name_of_model');
$CI->name_of_model->function_name();

This is untested but should work.
#3

[eluser]mattots[/eluser]
Thanks waldmeister, that works!




Theme © iAndrew 2016 - Forum software by © MyBB