DerekAllard.com

Auto-Loading Models in CodeIgniter

Quietly added, CodeIgniter now has the ability to auto-load Models.  This is super handy if you have a site-wide need for a model, say a “settings” model, or an “auth” model.  The feature will be in the next CI release*, but you can grab it now if you want to.  Just get a new copy of the Loader from the SVN (here it is), and add this into your config/autoloader.php file.

$autoload['model'= array('model1_model''model2_model'); 

And magically, your models will be autoloaded ;)  There are updated docs in the SVN also.  If you are a true nerd, you’ll might enjoy the “foreach” variables on line 112.  Well, I thought it was clever.  Anyhow, enjoy it while its hot, I have no idea if those variable names will stay the same when we release.  (Oh man, my nerd humour can be pretty lame…)

Features are slowly tricking into the SVN, and we’ve got many more goodies waiting to be released.

* I just wanted to drop an update, there is no release date established yet.

This entry was made on and filed into CodeIgniter.

Comments

Henrik wrote on

I’ve been thinking about this very topic the last few days and thought that there must be a way to auto-load them… well… now I don’t have to.

Great work folks, keep it up!

CodeOfficer wrote on

haha, good one. great update. this new feature will be well received. I’ve been auto-loading models via a post controller constructor hook up till now.

Speaking of auto-loading, I wrote a CI Javascripts library recently that accepts an array of JS files you would like to load (from a $config[‘javascripts’]), as well it lets you load JS files as you go in your controller ... saving them in an array for later use by the View.

If anyone is interested: http://pastie.textmate.org/pastes/105485

Shane Ross wrote on

That is going to be awesome. I look forward to using that.

Shadowhand wrote on

Someone should really do a “trim trailing whitespace” on the entire CI source. It seems none of the folks at Ellis Labs have “show invisibles” turned on.

Glad that CI is showing a little life again!

Adam Price wrote on

I have made the changes you said, but on auto loading a model it says “Call to a member function on a non-object”. Now the function its talking about is $this->db-get(); So what seems to be happening is my database isn’t being loaded before the model is?

Any idea?

barbazul wrote on

We really needed a way to autoload babes ;)

Great job Derek!!!