CodeIgniter Forums
Can a helper load a model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Can a helper load a model? (/showthread.php?tid=6039)

Pages: 1 2


Can a helper load a model? - El Forum - 02-12-2008

[eluser]taewoo[/eluser]
Is this possible? Example?


Can a helper load a model? - El Forum - 02-12-2008

[eluser]wiredesignz[/eluser]
Have you tried? Did it work?


Can a helper load a model? - El Forum - 02-12-2008

[eluser]taewoo[/eluser]
Don't even know where to look...? Thus, no idea.


Can a helper load a model? - El Forum - 02-12-2008

[eluser]wiredesignz[/eluser]
Helpers generally assist by providing specific functions to use throughout your application, They aren't meant to be used to load CI objects.


Can a helper load a model? - El Forum - 02-12-2008

[eluser]ejangi[/eluser]
wiredesignz is right - helpers aren't "meant" to access models, but they can through the CI singleton/controller like so:
Code:
function my_helper_function()
{
    $CI =& get_instance();
    $CI->load->model('mymodel');
    $var = $CI->mymodel->whatever();
}
The get_instance() function is in the global namespace and can be called from just about anywhere, returning the base CodeIgniter object (which all the models/libraries are properties of).


Can a helper load a model? - El Forum - 02-12-2008

[eluser]wiredesignz[/eluser]
I hesitated to post something like that ucant, my reason is that the CI super object is still being used to load the model (as you said), so it may as well be done properly from the controller or even autoloaded.

It's just a matter of thinking the process through when building an application. (Do I really wan't to do this here?, is there a better way?)


Can a helper load a model? - El Forum - 02-12-2008

[eluser]taewoo[/eluser]
I just tried. No luck

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: helpers/ajax_rating_helper.php

Line Number: 11



Can a helper load a model? - El Forum - 02-12-2008

[eluser]ejangi[/eluser]
... And what is on line 11?


Can a helper load a model? - El Forum - 02-12-2008

[eluser]taewoo[/eluser]
some function declared in the model..

PS: model was tested and it's fine..


Can a helper load a model? - El Forum - 02-12-2008

[eluser]ejangi[/eluser]
If you post the code, I can diagnose the issue, otherwise there's not much more I can do I'm afraid.