Welcome Guest, Not a member yet? Register   Sign In
Loading library/model in a constructor
#1

[eluser]Shay Falador[/eluser]
Well, this is not the first time I am having problems with communicating between models to other models and libraries.
The last time I saw this kind of "bug" was when I wanted to call a model from another model, it didn't work. But a little change in the loader library fixed it.

Now I am standing against a bigger problem.. Which I partly solved.
When I am loading a library (in this case form validation) in the constructor of a model I can't use it right away.
In order to use I muse run this code immediately after loading it:
Code:
$this->_assign_libraries();

I went deep to the code and found out that every model name is assigned into an array, and whenever loading a new model or library CI goes over that array and calls _assign_libraries() for each one of these models.
The problem is that because I loaded the library in the constructor, it is not yet in that array, and therefore not included in the foreach loop.

So, I needed to manually call the method, but maybe we can find another-better solution for this problem.

Thanks for reading!
Shay
#2

[eluser]summery[/eluser]
I was pretty confused by this behavior too. Thanks for your post providing the solution.
#3

[eluser]CI Coder[/eluser]
I've had this problem lately while working on a project. I have decided to add this code at the end of Load::model() method:

<code>
foreach ( $this->_ci_models as $m ){
$CI->$m->_assign_libraries();
}
</code>

I's annoying to have to type $this->_assign_libraries() all the time.

All the best!




Theme © iAndrew 2016 - Forum software by © MyBB