如何加载非默认模型? [英] How to load non-default models?

查看:114
本文介绍了如何加载非默认模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CakePHP2.x中,我经常在控制器中使用 $ uses 属性,但似乎此属性在CakePHP 3.0中不再可用。

In CakePHP2.x, I have frequently used $uses attribute in controllers but it seems that this attribute is no longer available in CakePHP 3.0.

我知道加载模型不是默认模型的唯一方法是使用 loadModel()方法。
这是推荐的方式加载模型?或者有任何其他方式加载模型?

The only way I know to load models which is not default one is to use loadModel() method. Is this recommended way to load models? Or is there any other way to load models?

推荐答案

您的观察是正确的,没有 $ uses ,而不是与控制器不匹配的模型/表( PostsTable PostsController ),必须明确加载。

Your observations are correct, there is no $uses property anymore, instead models/tables that do not match the controller (ex PostsTable for PostsController) and are not available via associations, must be loaded explicitly.

这可以使用


  • < a href =http://book.cakephp.org/3.0/en/controllers.html#Cake%5CController%5CController::loadModel =nofollow> Controller :: loadModel () ,将实例作为属性添加到控制器。

  • $ b
  • Controller::loadModel() which adds the instance to the controller as a property.
  • or TableRegistry::get() which just returns the instance.

所以这是对你的问题的答案, Controller :: loadModel() p>

So this is kind of a yes to your question, Controller::loadModel(), ie

$this->loadModel('Name');

是将模型/表实例添加到控制器作为属性的可行方法,至少在默认配置基本上是

is a viable way of adding model/table instances to your controller as properties, wich at least in the default configuration, is essentially the shorthand for

$this->Name = TableRegistry::get('Name')

这篇关于如何加载非默认模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆