EllisLab text mark
Advanced Search
     
Autoloading Models
Posted: 01 November 2007 07:34 PM   [ Ignore ]
Avatar
Joined: 2007-09-01
820 posts

After implementing the auto loading feature described here I get an error. What happens is a model is auto loaded and in it the model uses the database call ($this->db->get();) to fetch records.

What I think is happening is when this error occurs the database library hasn’t been loaded yet, since in the loader.php class it is loaded after the models.

How can I auto load a model which needs to use the database and not get this error? I did try putting $this->load->database() into the constructor function of the model but that did nothing either.

 Signature 

Kaydoo - A day in the life of a developer
BackendPro Control Panel

 
Posted: 01 November 2007 07:45 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-03-23
3194 posts

Try the advice here: http://ellislab.com/forums/viewthread/63795/

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design

 
Posted: 02 November 2007 12:35 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-11
103 posts

i noticed this change a week ago or so and at the moment i thought the autoloading order felt wrong for some reason.
Now I see why.

Models work somewhat different than the other objects that get appended to the controller object: enything on the controller gets appended to them also.
So, when working with models, you assume that any loaded library is available within the model. That should remain true to autoloaded libraries/models since the process of autoloading is “transparent” to the developer. The developer shouldn’t really care of the autoloading order.

I tried moving the model autoloading to the end of the _ci_autoloader method. It didn’t work at first, but then I realised that in the haste of preparing the example I forgot to call parent::Model() at the begining of the model constructor.
Added that line and now the fix seems to work smoothly

what do you think?

 Signature 

Security Code Review