When to load models?

Hii there,

This is a bit of a smaller question, but I wonder when I should load the models.
We are working on a project here and I’ve always learned (and done it this way) to load the models in the initialize method of a controller.
However, my colleagues have written their code to only load the model when they actually need it.

Is this a bad practice or should it not matter?

I’d say if it’s a model that’s used by all (or nearly all) the actions in the controller, then load it in initialize; this cuts down redundant code. If it’s something that’s only used sporadically, load it when you need it; this cuts down on unnecessary execution.

My usual method is to reference things through locally-known associations where it’s easy and a short chain. For example, if I need to access the Profiles table when I’m in the Users controller, $this->Users is already available by default, and they’re associated, so I use $this->Users->Profiles instead of explicitly loading it.


That makes sense yes.
I’ll throw it in the discussion :slight_smile:
Thanks for your information.