简体   繁体   English

我应该使用App :: import('Model',…)还是ClassRegistry(…)?

[英]Should I use App::import('Model', …) or ClassRegistry(…)?

Because of other answers ( like this ), I'm just wanting to clarify what should be used in CakePHP 1.3 . 由于有其他答案( 像这样 ),我只是想弄清楚CakePHP 1.3应该使用什么。

Specifically, I have a situation that calls for a Model to depend on another, so from a method in that Model I'd like to load another, do some stuff with the info, etc. 具体来说,我有一种情况需要模型依赖于另一个模型,因此我想从该模型中的方法中加载另一个模型,对信息进行一些处理,等等。

The documentation for the App Class says: App类文档说:

In previous versions there were different functions for loading a needed class based on the type of class you wanted to load. 在以前的版本中,有不同的函数根据您要加载的类的类型来加载所需的类。 These functions have been deprecated, all class and library loading should be done through App::import() now. 这些函数已被弃用,所有类和库的加载现在都应通过App :: import()完成。

I'm assuming this covers the use of ClassRegistry , etc, but I just want to it to be clear, and certain: 我假设这涵盖了ClassRegistry等的使用,但我只是想清楚一点,并确定:

Should I use App::import('Model', ...) to utilize one Model from another, or something else? 我应该使用App::import('Model', ...)来利用另一个模型,还是其他模型? If something else, what? 如果还有别的什么?

It appears that, even two years since 2008, the best method is to use ClassRegistry::init() , despite the cited documentation. 看来,即使自2008年以来的两年,最好的方法还是使用ClassRegistry::init() ,尽管引用了文档。

This is made evident in the actual API/documentation for the specific classes/methods. 在特定类/方法的实际API /文档中,这一点很明显。

App::import()

Finds classes based on $name or specific file(s) to search. 根据$ name或要搜索的特定文件查找类。 Calling App::import() will not construct any classes contained in the files. 调用App :: import()将不会构造文件中包含的任何类。 It will only find and require() the file. 它将仅查找和require()文件。

ClassRegistry::init()

Loads a class, registers the object in the registry and returns instance of the object. 加载一个类,在注册表中注册该对象并返回该对象的实例。

Examples Simple Use: Get a Post model instance ClassRegistry::init('Post'); 示例简单使用:获取一个Post模型实例ClassRegistry :: init('Post');。

As you can see, even the API Documentation points out examples of using ClassRegistry to load models, instantiating them for you, as opposed to App::import (which does much less), and despite the changed wording in the CakePHP "Book" documentation. 如您所见,即使CakePHP“ Book”文档中的措辞有所更改,即使API文档也指出了使用ClassRegistry加载模型,为您实例化模型的示例,而不是App::import (这样做要少得多) 。

If you can relate the models then the best way is to Dynamically bind the relations using 如果可以关联模型,那么最好的方法是使用以下方式动态绑定关系

$this->bindModel("hasOne" => array("Model2")) . $this->bindModel("hasOne" => array("Model2"))

If you can't relate the model and you want to use the second model in just one occurrence then you can use 如果您无法关联模型,并且只想一次使用第二个模型,则可以使用

ClassRegistry::init('Model2')->find('allThatIWant');

if you want to use it in several occurrence then you must try 如果您想多次使用它,则必须尝试

$this->model2 = & ClassRegistry::init('Model2') 
$this->model2->find('allThatIWant');

As of 2.6.x of course it is ClassRegistry::init() still. 从2.6.x开始,它当然仍然是ClassRegistry :: init()。

There is a major difference. 有很大的不同。 App::import will just include/require it. App :: import只会包含/需要它。 On the other hand ClassRegistry::init() will instantiate it and fetch you a fully loaded object of the model. 另一方面, ClassRegistry :: init()将实例化它并为您获取模型的完全加载的对象。

So to say, for example you loaded a model in beforeFilter of your AppController. 可以这么说,例如,您在AppController的beforeFilter中加载了一个模型。 You add some custom properties to it using $this->Model->__something . 您可以使用$ this-> Model-> __ something向其中添加一些自定义属性。 Now you do call ClassRegistry::init('Model') somewhere where you do not have $controller object available, for example, in a behavior. 现在,您确实在没有$ controller对象可用的地方调用ClassRegistry :: init('Model'),例如,在行为中。 The object returned by ClassRegistry::init('Model') will have your custom property $this->Model->__something in tact. 由ClassRegistry :: init('Model')返回的对象将具有自定义属性$ this-> Model- > __ something

Btw, $controller->loadModel() seems the ideal way to load model where you have a $controller object available, for example in your components. 顺便说一句, $ controller-> loadModel()似乎是加载模型的理想方法,例如,在您的组件中有$ controller对象可用的情况下。

$this->loadModel('model name')$this->loadModel('model name') ,除非您需要整个控制器使用,然后只需在模型中定义关系,例如hasone,belongsed ...,然后调用$this->model->model2

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

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