user765368
user765368

Reputation: 20346

cakephp use another model inside current model

I know I can use another model inside a controller by doing $this->loadModel("MyModel"), but how do I do this inside another Model? I tried to use loadModel but it didn't work.

Any idea?

Thank you

Upvotes: 11

Views: 18242

Answers (3)

Ehtesham
Ehtesham

Reputation: 2985

You can use the following code to export a model that is not associated with the current model in any way:

App::import('Model', 'MyModel');
$my_model = new MyModel();

If MyModel is associated with current model you could use the chaining e.g. $this->SomeModel->MyModel

Upvotes: 5

Paulo Peres Junior
Paulo Peres Junior

Reputation: 559

You don't need to import anything. Just do this:

$my_model = new MyModel();
//Then
$my_model->read(null,$id);

Upvotes: 0

Costa
Costa

Reputation: 4969

Easier is:

$my_model = ClassRegistry::init('MyModel');

More details: Can I use one model inside of a different model in CakePHP?

Upvotes: 28

Related Questions