假设我有两个这样的控制器Table1sController
, and Table2sController
。
对应型号:Table1sModel
, Table2sModel
.
In the Table1sController
, 我懂了:
$this->Table1sModel->action();
假设我想访问其中的一些数据Table2sModel
.
怎么可能在Table1sController
?
我在 Table1sController 中尝试过:
$this->Table2sModel->action();
但我收到了这样的错误消息:
未定义的属性:Table1sController::$Table2sModel
有几种方法可以到达这里。
如果您的模型已定义关联(hasMany 等),那么您可以使用以下方法访问该模型的方法(假设您位于 Model1Controller 中):
$this->Model1->Model2->method();
如果两个模型之间没有模型关联,但您希望能够使用 Model2 的方法,则可以在 model1Controller 的 $uses 属性中添加一个条目。看http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses
最后,如果它是临时连接(您不希望每次都加载其他模型的开销,因为您很少访问 model2),请查看手册中有关动态创建/销毁关联的部分,网址为http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)