tags:

views:

68

answers:

4

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

+1  A: 

You 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.

A. M.
If you need to use them across your entire controller, I'd just try $uses. After all, we're using cake for the programmer's performance, not the machine's. If it's just in one or two methods, try something else so it doesn't drag down your other methods.
Travis Leleu
Yes, you have a point. I was assuming that he doesn't need all 6 models for the entire controller.
A. M.
+1  A: 

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.

Leo
A: 

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");
SpawnCxy
can u provide any link to know more on this or you only tell me on how i can use it
Web Developer
@Web Developer,see my update,plz.
SpawnCxy
on php5.x ClassRegistry::init('User')->find('all') works also, with no need to create new objects all over.
dogmatic69
@dogmatic69,I prefer to write it like this because it looks more like a model style and aslo no need to create more than one object.
SpawnCxy
+1  A: 

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:))

dr Hannibal Lecter