cakephp use another model inside current model
16,543
Solution 1
Easier is:
$my_model = ClassRegistry::init('MyModel');
More details: Can I use one model inside of a different model in CakePHP?
Solution 2
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
Author by
user765368
Updated on June 05, 2022Comments
-
user765368 almost 2 years
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
-
dmajka almost 3 yearsThis is the best way. If you use
App::import
orApp::uses
with model constructor then will be problem e.g. with TranslateBehavior. Reason:DboSource
class useClassRegistry::getObject
to get model and read virtual fields. So to prevent using two instances of the same model use this solution.