Welcome Guest, Not a member yet? Register   Sign In
using libraries in models
#11

[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();
$CI->load->library("MyLib");
$CI->mylib->setSomething("some value");

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.
#12

[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)...
#13

[eluser]David B.[/eluser]
Thanks onejaguar for that detailed explanation from my side, too. That helped alot!




Theme © iAndrew 2016 - Forum software by © MyBB