Hi,
i am in a situation where i need to fetch data from 6 models. i can't use associations as they are not related. So what should i use $uses
or request action
or any other solution
views:
68answers:
4You could try $uses, or, if you want to only load the models when and where you need them, you could use loadModel (http://book.cakephp.org/view/845/loadModel). LoadModel is better that $uses, performance wise.
I disagree with Travis. It is much better to use loadModel then you can be sure you're loading what you need when you need it. Who's to say you won't extend the model to include methods that don't require all those other models?
Cut and paste is a big boost to programmer performance.
I'd recommend you to use
ClassRegistry::init("model_name")
or
loadModel("model_name")
instead.
E.g.
To use a User
model
$this->User = ClassRegistry::init('User');
Or
$this->loadModel('User');
Then you can do some query like
$this->User->find("all");
As others recommended, you should use Controller::loadModel()
.
Additionaly, I'd suggest you make something like YourController::_loadMyMegaData()
which would load all the necessary models and set your data. This will avoid loading those models by accident and spare you any if blocks later on.
Also, avoid requestAction()
whenever possible, it's not only ugly, you also take a performance hit. (It has it's use of course, but that doesn't happen very often:))