Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

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.

I'm assuming this covers the use of ClassRegistry, etc, but I just want to it to be clear, and certain:

Should I use App::import('Model', ...) to utilize one Model from another, or something else? If something else, what?

like image 204
anonymous coward Avatar asked Mar 08 '11 20:03

anonymous coward


1 Answers

It appears that, even two years since 2008, the best method is to use ClassRegistry::init(), despite the cited documentation.

This is made evident in the actual API/documentation for the specific classes/methods.

App::import()

Finds classes based on $name or specific file(s) to search. Calling App::import() will not construct any classes contained in the files. It will only find and require() the file.

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');

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.

like image 111
anonymous coward Avatar answered Sep 21 '22 17:09

anonymous coward