Databasic v2.3.2

A basic wrapper class for the MySQL database engine. Contains methods for connecting to a database, fetching and returning results, building insert and update queries, optimization and more.

Loading Models specific to certain actions

Friday, February 6th 2009, 3:55am
Topics: Tutorials, CakePHP
Tags: Model, Action, Cakephp
Views: 8,560, Comments: 7

Recently I was working on a project, where a certain controller had over 15ish+ different models. The problem is that each action needed a different model, and I didn't want to load all those models at once. By using my common sense, loading that many models would put more of a strain on load time by having to initialize each model and bind them to the controller. I needed a quick way to load a model(s) specific only to a certain action. At first I tried putting $this->user[] = array('Model'); at the top of each action, but that didn't work and it didn't initialize "before" the action like it is supposed to. So what I came up with is a little statement trick that you can place in your beforeFilter(), which will load models specific to certain actions. Enjoy!

/**
 * Executed before each action
 */
function beforeFilter() {
	parent::beforeFilter();

	// Load action specific models
	switch ($this->params['action']) {
		case 'friends':		$models = array('Friend'); break;
		case 'messages':	$models = array('Message'); break;
		case 'blog':		$models = array('Entry', 'Comment'); break;
	}
	
	if (!empty($models)) {
		foreach ($models as $model) {
			$this->loadModel($model);
		}
	}
}


Also it would be best to disable models be default in controller. This way it doesn't load a certain model twice, or overwrite the defaults.

class TestController extends AppController {
	var $uses = NULL;
}
Related Entries:

7 Comments

10 / 2 = ?
Allowed: [code] [b] [i] [u]
  • Marek Kebza
    kebza.cz
    Feb 6th 2009, 04:13
    1 Do you know ClassRegistry::init(...) ? It was added to CakePHP 1.2 and it is very useful for that. This looks too complicated for me.
  • Miles Johnson
    www.milesj.me
    Feb 6th 2009, 14:24
    2 @Marek - loadModel() calls ClassRegistry::init() by itself, and is part of the main controller class. loadModel() also has built in persistence, error logging and caching.

    You can call ClassRegistry like you suggested, but I don't think it does what I just noted above.
  • Juan Basso
    blog.cakep...brasil.org
    Mar 12th 2009, 04:23
    3 Until "$uses = NULL", use "$uses = array()". See the post of Cakebaker: http://cakebaker.42dh.com/2007/10/30/what-i-didnt-knew-about-controllers-without-models/
  • Miles Johnson
    www.milesj.me
    Mar 12th 2009, 13:44
    4 @Juan Basso - Hmm that article is quite interesting. Its weird though, I haven't run into any problems like that yet, and I've been using this technique for quite a while. Maybe its because he used beforeRender() and not beforeFilter(). Either way ill try using $uses = array() from now on.
  • JM
    Mar 14th 2009, 10:22
    5 Hi Miles,

    how can you be sure that that the param passed corresponds to an existing model prior to make the classregistry/loadmodel ? if it doesnt exist the result is weird, so better don't reach to the execution ...

    Maybe with file_exists can check if at least the Model file exist, but hard to check if the model is inside a plugin for example ...

    Do you know any other way to check before try to load ?

    JM
  • Miles Johnson
    www.milesj.me
    Mar 14th 2009, 13:19
    6 @Jm - Well you should know what models exist when you are adding them into the models array for that action.

    Also loadModel() calls ClassRegistry::init(). The loadModel() checks to see if there is a cached model, if not it calls ClassRegistry. The ClassRegistry method will check if the model exists, if the model does not it will return the AppModel object.

    Most of the logic checking is already existent, no need to do it again.
  • mossyTomTwich
    silantiev.com
    May 4th 2009, 00:32
    7 Best reviews of the day about Depression, Essay on teen depression, Depression help ways and Major depression symptoms.