5.7.1 Understanding Models
A Model represents your data model and in object-oriented programming is an object that represents a "thing", like a car, a person, or a house. A blog, for example, may have many blog posts and each blog post may have many comments. The Blog, Post, and Comment are all examples of models, each associated with another.
Here is a simple example of a model definition in CakePHP:
<?php
class Ingredient extends AppModel {
var $name = 'Ingredient';
}
?>
<?phpclass Ingredient extends AppModel {var $name = 'Ingredient';}?>
With just this simple declaration, the Ingredient model is bestowed with all the functionality you need to create queries along with saving and deleting data. These magic methods come from CakePHP's model inheritance. The Ingredient model extends the application model, AppModel, which extends CakePHP's internal Model class. It is this internal Model class that bestows the functionality onto your custom model, Ingredient.
This intermediate class, AppModel, is empty and resides within the /cake/ folder, by default. The AppModel allows you to define functionality that should be made available to all models within your application. To do so, you need to create your own app_model.php file that resides in the root of the /app/ folder. Creating a project using Bake will automatically generate this file for you.
See also Behaviors for more information on how to apply similar logic to multiple models.
Note: The $name property is necessary for PHP4 but optional for PHP5.
With your model defined, it can be accessed from within your Controller. CakePHP will automatically make the model available for access when its name matches that of the controller. For example, a controller named IngredientsController will automatically initialize the Ingredient model and attach it to the controller at $this->Ingredient.
<?php
class IngredientsController extends AppController {
function index() {
//grab all ingredients and pass it to the view:
$ingredients = $this->Ingredient->find('all');
$this->set('ingredients', $ingredients);
}
}
?>
<?phpclass IngredientsController extends AppController {function index() {//grab all ingredients and pass it to the view:$ingredients = $this->Ingredient->find('all');$this->set('ingredients', $ingredients);}}?>
Associated models are available through the main model. In the following example, Recipe has an association with the Ingredient model.
$this->Recipe->Ingredient->find('all'); $this->Recipe->Ingredient->find('all');
As you may have seen in Controllers, you can attach multiple models to the controller and accessible directly from the controller. In the following example, both Recipe and User are accessible from the current controller.
<?php
class RecipeController extends AppController {
var $uses = array('Recipe', 'User');
function index() {
$this->Recipe->find('all');
$this->User->find('all');
}
}
?>
<?phpclass RecipeController extends AppController {var $uses = array('Recipe', 'User');function index() {$this->Recipe->find('all');$this->User->find('all');}}?>
If you haven't added the model via the $uses property then you'll need to manually import the model and instantiate it from within the action.
<?php
class RecipeController extends AppController {
var $uses = array('Recipe');
function index() {
$this->Recipe->find('all');
App::import('Model', 'User');
$user = new User();
$user->find('all');
}
}
?>
<?phpclass RecipeController extends AppController {var $uses = array('Recipe');function index() {$this->Recipe->find('all');App::import('Model', 'User');$user = new User();$user->find('all');}}?>
