aWebDeveloper
aWebDeveloper

Reputation: 38352

cakephp $uses or request action

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

Upvotes: 1

Views: 1908

Answers (6)

Ankit
Ankit

Reputation: 1

You can use the following:

$this->loadModel('ModelName'); 
$this->ModelName->find();

If you are retrieving only single field than you can use virtual fields

Upvotes: 0

Yuri Ghensev
Yuri Ghensev

Reputation: 2545

If you are going to need those models for only one find call with associtaions needed, I recommend using bindModel, see Creating-and-Destroying-Associations-on-the-Fly.

Upvotes: 2

Leo
Leo

Reputation: 6571

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.

Upvotes: 1

Young
Young

Reputation: 8356

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");

Upvotes: 2

dr Hannibal Lecter
dr Hannibal Lecter

Reputation: 6721

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

Upvotes: 1

A. M.
A. M.

Reputation: 565

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.

Upvotes: 1

Related Questions