![]() |
using libraries in models - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: using libraries in models (/showthread.php?tid=8740) Pages:
1
2
|
using libraries in models - El Forum - 06-02-2008 [eluser]onejaguar[/eluser] The short answer: If you want to use a library in your model's constructor you either have to load it in the controller before you load the model, or better yet use Code: $CI =& get_instance(); Long Answer: The reason is that CodeIgniter manually copies references to all the loaded libraries so that the model can access them with $this->. When a model is created, the "_assign_libraries()" method is called, assigning all currently loaded libraries to the model. Likewise whenever you load a library the function "_ci_assign_to_models()" gets called which assigns the library to all loaded models. The problem is your model isn't recognized as loaded until "__construct()" has finished, so any libraries you load in the construct aren't assigned in the initial "_assign_libraries()" and won't be attached to your model by "_ci_assign_to_models()" because the model isn't loaded yet. Confusing I know, but it makes sense when you think it through. using libraries in models - El Forum - 06-02-2008 [eluser]crumpet[/eluser] thanks a lot for the detailed explanation.. i think i understand I was trying to load the library in the model's constructor and that doesn't work loading the model in the function of the model seems to work (or it may be that I am actually loading the library in the constructor for the controller -- have to check that one out and get back to you)... using libraries in models - El Forum - 06-03-2008 [eluser]David B.[/eluser] Thanks onejaguar for that detailed explanation from my side, too. That helped alot! |