简体   繁体   English

cakephp在当前模型中使用另一个模型

[英]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? 我知道我可以通过执行$this->loadModel("MyModel")在控制器中使用另一个模型,但是如何在另一个模型中执行此操作? I tried to use loadModel but it didn't work. 我试图使用loadModel但它没有用。

Any idea? 任何的想法?

Thank you 谢谢

Easier is: 更容易:

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

More details: Can I use one model inside of a different model in CakePHP? 更多细节: 我可以在CakePHP中的不同模型中使用一个模型吗?

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 eg $this->SomeModel->MyModel 如果MyModel与当前模型相关联,您可以使用链接,例如$this->SomeModel->MyModel

You don't need to import anything. 您不需要导入任何东西。 Just do this: 这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM