Loading library/model in a constructor - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Loading library/model in a constructor (/showthread.php?tid=27523) |
Loading library/model in a constructor - El Forum - 02-13-2010 [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 Loading library/model in a constructor - El Forum - 03-02-2010 [eluser]summery[/eluser] I was pretty confused by this behavior too. Thanks for your post providing the solution. Loading library/model in a constructor - El Forum - 01-27-2011 [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! |