I am a cakePHP newbie, who has just been assigned to work on an existing system built with cakePHP 1.3. As I get familiar with the project and it's contents their are snippets of code I don't quite understand. Again and again (usually in a controller file) I see these:
'$this->model'
'$this->Model'
'$this->{$this->model}'
Searching google, the cake docs, and stackoverflow has yet to reveal a concise explanation of what exactly these strings are referring to. Many of the controllers I am working with have more than one model with which they interact, how do I know which one these snippets are invoking?
For instance web_forms_controller.php has 3 models which go with it web_forms.php, web_forms_field.php, and web_forms_submission.php. I feel like I am missing something basic here, but RTM has yet to reveal the answer I am after.
So in the above instance I am guessing that '$this->model' and '$this->Model' seen in web_forms_controller.php refer to the web_forms.php model. What '$this->{$this->model}' is a reference to in this case I have no idea.
First off CakePHP uses the "convention over configuration" convention. This heavily explains a lot! :) There is a thingy called Inflector - it "just" 'pluralizes and singularizes English nouns. Used by Cake's naming conventions throughout the framework.'
This combined with the conventions and a few key principles makes it possible that Cake tells which Class to bind to which other.
So, $this->model
in a controller will be the Model directly associated with this Controller. For example in a UsersController
, $this->model
would be the User
model.
Other binded models are defined in the $uses
property of the Controller. Through $this->model
you call only the controller's own model. To call another binded model (for ex Post
) use:
$this->Post->findAll();
$this->model
i just a convenient shortcut from any given controller.