EllisLab text mark
Advanced Search
     
Loading library/model in a constructor
Posted: 13 February 2010 09:00 AM
Avatar
Joined: 2009-11-22
41 posts

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:

$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

 Signature 

Shay Ben Moshe - Web Developer.
Exalted Web (my portfolio - Hebrew)
Zend Certified Engineer

 
Posted: 02 March 2010 03:00 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-11-13
31 posts

I was pretty confused by this behavior too.  Thanks for your post providing the solution.

 
Posted: 27 January 2011 09:08 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-11-06
46 posts

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:


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

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

All the best!