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.


Messages In This Thread
using libraries in models - by El Forum - 05-29-2008, 01:47 PM
using libraries in models - by El Forum - 05-29-2008, 01:52 PM
using libraries in models - by El Forum - 05-29-2008, 02:16 PM
using libraries in models - by El Forum - 05-29-2008, 07:02 PM
using libraries in models - by El Forum - 05-29-2008, 07:54 PM
using libraries in models - by El Forum - 05-29-2008, 08:08 PM
using libraries in models - by El Forum - 05-29-2008, 08:14 PM
using libraries in models - by El Forum - 05-30-2008, 11:18 AM
using libraries in models - by El Forum - 05-30-2008, 11:34 AM
using libraries in models - by El Forum - 06-02-2008, 07:42 AM
using libraries in models - by El Forum - 06-02-2008, 03:30 PM
using libraries in models - by El Forum - 06-02-2008, 10:35 PM
using libraries in models - by El Forum - 06-03-2008, 01:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB