3.7.6 Associations: Linking Models Together
One of the most powerful features of CakePHP is the ability to link relational mapping provided by the model. In CakePHP, the links between models are handled through associations.
Defining relations between different objects in your application should be a natural process. For example: in a recipe database, a recipe may have many reviews, reviews have a single author, and authors may have many recipes. Defining the way these relations work allows you to access your data in an intuitive and powerful way.
The purpose of this section is to show you how to plan for, define, and utilize associations between models in CakePHP.
While data can come from a variety of sources, the most common form of storage in web applications is a relational database. Most of what this section covers will be in that context.
3.7.6.1 Relationship Types
The four association types in CakePHP are: hasOne, hasMany, belongsTo, and hasAndBelongsToMany (HABTM).
| Relationship | Association Type | Example |
|---|---|---|
| one to one | hasOne | A user has one profile. |
| one to many | hasMany | Users in a system can have multiple recipes. |
| one to many | belongsTo | A recipe belongs to a user. |
| many to many | hasAndBelongsToMany | Recipes have, and belong to many tags. |
Associations are defined by creating a class variable named after the association you are defining. The class variable can sometimes be as simple as a string, but can be as complete as a multidimensional array used to define association specifics.
<?php
class User extends AppModel {
var $name = 'User';
var $hasOne = 'Profile';
var $hasMany = array(
'Recipe' => array(
'className' => 'Recipe',
'conditions' => 'Recipe.approved = 1',
'order' => 'Recipe.created DESC'
)
);
}
?>
<?phpclass User extends AppModel {var $name = 'User';var $hasOne = 'Profile';var $hasMany = array('Recipe' => array('className' => 'Recipe','conditions' => 'Recipe.approved = 1','order' => 'Recipe.created DESC'));}?>
In the above example, the first instance of the word 'Recipe' is what is termed an 'Alias'. This is an identifier for the relationship and can be anything you choose. Usually, you will choose the same name as the class that it references. However, aliases must be unique both within a single model and on both sides of a belongsTo/hasMany or a belongTo/hasOne relationship. Choosing non-unique names for model aliases can cause unexpected behavior.
3.7.6.2 hasOne
Let’s set up a User model with a hasOne relationship to a Profile model.
First, your database tables need to be keyed correctly. For a hasOne relationship to work, one table has to contain a foreign key that points to a record in the other. In this case the profiles table will contain a field called user_id. The basic pattern is:
| Relation | Schema |
|---|---|
| Apple hasOne Banana | bananas.apple_id |
| User hasOne Profile | profiles.user_id |
| Doctor hasOne Mentor | mentors.doctor_id |
The User model file will be saved in /app/models/user.php. To define the ‘User hasOne Profile’ association, add the $hasOne property to the model class. Remember to have a Profile model in /app/models/profile.php, or the association won’t work.
<?php
class User extends AppModel {
var $name = 'User';
var $hasOne = 'Profile';
}
?>
<?phpclass User extends AppModel {var $name = 'User';var $hasOne = 'Profile';}?>
There are two ways to describe this relationship in your model files. The simplest method is to set the $hasOne attribute to a string containing the classname of the associated model, as we’ve done above.
If you need more control, you can define your associations using array syntax. For example, you might want to sort related rows in descending order according to a date, or you might want to limit the association to include only certain records.
<?php
class User extends AppModel {
var $name = 'User';
var $hasOne = array(
'Profile' => array(
'className' => 'Profile',
'conditions' => 'Profile.published = 1',
'dependent' => true
)
);
}
?>
<?phpclass User extends AppModel {var $name = 'User';var $hasOne = array('Profile' => array('className' => 'Profile','conditions' => 'Profile.published = 1','dependent' => true));}?>
Possible keys for hasOne association arrays include:
- className: the classname of the model being associated to the current model. If you’re defining a ‘User hasOne Profile’ relationship, the className key should equal ‘Profile.’
- foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple hasOne relationships. The default value for this key is the underscored, singular name of the current model, suffixed with ‘_id’. In the example above it would default to 'user_id'.
- conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “Profile.approved = 1” is always better than just “approved = 1.”
- fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
- dependent: When the dependent key is set to true, and the model’s delete() method is called with the cascade parameter set to true, associated model records are also deleted. In this case we set it true so that deleting a User will also delete her associated Profile.
Once this association has been defined, find operations on the User model will also fetch a related Profile record if it exists:
//Sample results from a $this->User->find() call.
Array
(
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[Profile] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
[created] => 2007-05-01 10:31:01
)
)
3.7.6.3 belongsTo
Now that we have Profile data access from the User model, let’s define a belongsTo association in the Profile model in order to get access to related User data. The belongsTo association is a natural complement to the hasOne and hasMany associations: it allows us to see the data from the other direction.
When keying your database tables for a belongsTo relationship, follow this convention:
| Relation | Schema |
|---|---|
| Banana belongsTo Apple | bananas.apple_id |
| Profile belongsTo User | profiles.user_id |
| Mentor belongsTo Doctor | mentors.doctor_id |
If a model(table) contains a foreign key, it belongsTo the other model(table).
We can define the belongsTo association in our Profile model at /app/models/profile.php using the string syntax as follows:
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = 'User';
}
?>
<?phpclass Profile extends AppModel {var $name = 'Profile';var $belongsTo = 'User';}?>
We can also define a more specific relationship using array syntax:
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
?>
<?phpclass Profile extends AppModel {var $name = 'Profile';var $belongsTo = array('User' => array('className' => 'User','foreignKey' => 'user_id'));}?>
Possible keys for belongsTo association arrays include:
- className: the classname of the model being associated to the current model. If you’re defining a ‘Profile belongsTo User’ relationship, the className key should equal ‘User.’
- foreignKey: the name of the foreign key found in the current model. This is especially handy if you need to define multiple belongsTo relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
- conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “User.active = 1” is always better than just “active = 1.”
- fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
- counterCache: (bool) If set to true the associated Model will automatically increase or decrease the “[singular_model_name]_count” field in the foreign table whenever you do a save() or delete(). The value in the counter field represents the numer of related rows.
Once this association has been defined, find operations on the Profile model will also fetch a related User record if it exists:
//Sample results from a $this->Profile->find() call.
Array
(
[Profile] => Array
(
[id] => 12
[user_id] => 121
[skill] => Baking Cakes
[created] => 2007-05-01 10:31:01
)
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
)
3.7.6.4 hasMany
Next step: defining a “User hasMany Comment” association. A hasMany association will allow us to fetch a user’s comments when we fetch a User record.
When keying your database tables for a hasMany relationship, follow this convention:
| Relation | Schema |
|---|---|
| User hasMany Comment | Comment.user_id |
| Cake hasMany Virtue | Virtue.cake_id |
| Product hasMany Option | Option.product_id |
We can define the hasMany association in our User model at /app/models/user.php using the string syntax as follows:
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = 'Comment';
}
?>
<?phpclass User extends AppModel {var $name = 'User';var $hasMany = 'Comment';}?>
We can also define a more specific relationship using array syntax:
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'conditions' => 'Comment.status = 1',
'order' => 'Comment.created DESC',
'limit' => '5',
'dependent'=> true
)
);
}
?>
<?phpclass User extends AppModel {var $name = 'User';var $hasMany = array('Comment' => array('className' => 'Comment','foreignKey' => 'user_id','conditions' => 'Comment.status = 1','order' => 'Comment.created DESC','limit' => '5','dependent'=> true));}?>
Possible keys for hasMany association arrays include:
- className: the classname of the model being associated to the current model. If you’re defining a ‘User hasMany Comment’ relationship, the className key should equal ‘Comment.’
- foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple hasMany relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
- conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “Comment.status = 1” is always better than just “status = 1.”
- fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
- order: An SQL fragment that defines the sorting order for the returned associated rows.
- limit: The maximum number of associated rows you want returned.
- offset: The number of associated rows to skip over (given the current conditions and order) before fetching and associating.
-
dependent: When dependent is set to true, recursive model deletion is possible. In this example, Comment records will be deleted when their associated User record has been deleted.
The second parameter of the
Model->delete()method must be set to true in order for recursive deletion to occur. - finderQuery: A complete SQL query CakePHP can use to fetch associated model records. This should be used in situations that require very custom results.
Once this association has been defined, find operations on the User model will also fetch related Comment records if they exist:
//Sample results from a $this->User->find() call.
Array
(
[User] => Array
(
[id] => 121
[name] => Gwoo the Kungwoo
[created] => 2007-05-01 10:31:01
)
[Comment] => Array
(
[0] => Array
(
[id] => 123
[user_id] => 121
[title] => On Gwoo the Kungwoo
[body] => The Kungwooness is not so Gwooish
[created] => 2006-05-01 10:31:01
)
[1] => Array
(
[id] => 123
[user_id] => 121
[title] => More on Gwoo
[body] => But what of the ‘Nut?
[created] => 2006-05-01 10:41:01
)
)
)
One thing to remember is that you’ll need a complimentary Comment belongsTo User association in order to get the data from both directions. What we’ve outlined in this section empowers you to get Comment data from the User. Adding the Comment belongsTo User association in the Comment model empowers you to get User data from the Comment model - completing the connection and allowing the flow of information from either model’s perspective.
3.7.6.5 hasAndBelongsToMany (HABTM)
Alright. At this point, you can already call yourself a CakePHP model associations professional. You’re already well versed in the three associations that take up the bulk of object relations.
Let’s tackle the final relationship type: hasAndBelongsToMany, or HABTM. This association is used when you have two models that need to be joined up, repeatedly, many times, in many different ways.
The main difference between hasMany and HABTM is that a link between models in HABTM is not exclusive. For example, we’re about to join up our Recipe model with a Tag model using HABTM. Attaching the “Italian” tag to my grandma’s Gnocci recipe doesn’t “use up” the tag. I can also tag my Honey Glazed BBQ Spaghettio’s with “Italian” if I want to.
Links between hasMany associated objects are exclusive. If my User hasMany Comments, a comment is only linked to a specific user. It’s no longer up for grabs.
Moving on. We’ll need to set up an extra table in the database to handle HABTM associations. This new join table’s name needs to include the names of both models involved, in alphabetical order. The contents of the table should be at least two fields, each foreign keys (which should be integers) pointing to both of the primary keys of the involved models.
| Relation | Schema |
|---|---|
| Recipe HABTM Tag | recipes_tags.recipe_id, recipes_tags.tag_id |
| Cake HABTM Fan | cakes_fans.cake_id, cakes_fans.fan_id |
| Foo HABTM Bar | bars_foos.foo_id, bars_foos.bar_id |
Table name are by convention in alphabetical order.
Once this new table has been created, we can define the HABTM association in the model files. We’re gonna skip straight to the array syntax this time:
<?php
class Recipe extends AppModel {
var $name = 'Recipe';
var $hasAndBelongsToMany = array(
'Tag' =>
array('className' => 'Tag',
'joinTable' => 'posts_tags',
'foreignKey' => 'recipe_id',
'associationForeignKey' => 'tag_id',
'conditions' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
?>
<?phpclass Recipe extends AppModel {var $name = 'Recipe';var $hasAndBelongsToMany = array('Tag' =>array('className' => 'Tag','joinTable' => 'posts_tags','foreignKey' => 'recipe_id','associationForeignKey' => 'tag_id','conditions' => '','order' => '','limit' => '','unique' => true,'finderQuery' => '','deleteQuery' => '','insertQuery' => ''));}?>
Possible keys for HABTM association arrays include:
- className: the classname of the model being associated to the current model. If you’re defining a ‘User hasMany Comment’ relationship, the className key should equal ‘Comment.’
- joinTable: The name of the join table used in this association (if the current table doesn’t adhere to the naming convention for HABTM join tables).
- foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple HABTM relationships. The default value for this key is the underscored, singular name of the other model, suffixed with ‘_id’.
- associationForeignKey: the name of the foreign key found in the current model. This is especially handy if you need to define multiple HABTM relationships. The default value for this key is the underscored, singular name of the current model, suffixed with ‘_id’.
- conditions: An SQL fragment used filter related model records. It’s good practice to use model names in SQL fragments: “Comment.status = 1” is always better than just “status = 1.”
- fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.
- order: An SQL fragment that defines the sorting order for the returned associated rows.
- limit: The maximum number of associated rows you want returned.
- unique: If true (default value) cake will first delete existing relationship records in the foreign keys table before inserting new ones, when updating a record. So existing associations need to be passed again when updating.
- offset: The number of associated rows to skip over (given the current conditions and order) before fetching and associating.
- finderQuery, deleteQuery, insertQuery: A complete SQL query CakePHP can use to fetch, delete, or create new associated model records. This should be used in situations that require very custom results.
Once this association has been defined, find operations on the Recipe model will also fetch related Tag records if they exist:
//Sample results from a $this->Recipe->find() call.
Array
(
[Recipe] => Array
(
[id] => 2745
[name] => Chocolate Frosted Sugar Bombs
[created] => 2007-05-01 10:31:01
[user_id] => 2346
)
[Tag] => Array
(
[0] => Array
(
[id] => 123
[name] => Breakfast
)
[1] => Array
(
[id] => 124
[name] => Dessert
)
[2] => Array
(
[id] => 125
[name] => Heart Disease
)
)
)
Remember to define a HABTM association in the Tag model if you’d like to fetch Recipe data when using the Tag model.
It is also possible to execute custom find queries based on HABTM relationships. Consider the following examples:
Assuming the same structure in the above example (Recipe HABTM Tag), let's say we want to fetch all Recipes with the tag 'Dessert', one potential (wrong) way to achieve this would be to apply a condition to the association itself:
$this->Recipe->bindModel(array(
'hasAndBelongsToMany' => array(
'Tag' => array('conditions'=>array('Tag.name'=>'Dessert'))
)));
$this->Recipe->find('all');
$this->Recipe->bindModel(array('hasAndBelongsToMany' => array('Tag' => array('conditions'=>array('Tag.name'=>'Dessert')))));$this->Recipe->find('all');
//Data Returned
Array
(
0 => Array
{
[Recipe] => Array
(
[id] => 2745
[name] => Chocolate Frosted Sugar Bombs
[created] => 2007-05-01 10:31:01
[user_id] => 2346
)
[Tag] => Array
(
[0] => Array
(
[id] => 124
[name] => Dessert
)
)
)
1 => Array
{
[Recipe] => Array
(
[id] => 2745
[name] => Crab Cakes
[created] => 2008-05-01 10:31:01
[user_id] => 2349
)
[Tag] => Array
(
}
}
}
Notice that this example returns ALL recipes but only the "Dessert" tags. To properly achieve our goal, there are a number of ways to do it, all of which involve binding a temporary association to the join table and/or tag model.
$this->Recipe->bindModel(array('hasOne' => array('RecipesTag')));
$this->Recipe->find('all', array(
'fields' => array('Recipe.*'),
'conditions'=>array('RecipesTag.tag_id'=>124) // id of Dessert
));
$this->Recipe->bindModel(array('hasOne' => array('RecipesTag')));$this->Recipe->find('all', array('fields' => array('Recipe.*'),'conditions'=>array('RecipesTag.tag_id'=>124) // id of Dessert));
It's also possible to create an exotic association for the purpose of creating as many joins as necessary to allow filtering, for example:
$this->Recipe->bindModel(array(
'hasOne' => array(
'RecipesTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = RecipesTag.id')
)));
$this->Recipe->find('all', array(
'fields' => array('Recipe.*'),
'conditions'=>array('FilterTag.name'=>'Dessert')
));
$this->Recipe->bindModel(array('hasOne' => array('RecipesTag','FilterTag' => array('className' => 'Tag','foreignKey' => false,'conditions' => array('FilterTag.id = RecipesTag.id'))));$this->Recipe->find('all', array('fields' => array('Recipe.*'),'conditions'=>array('FilterTag.name'=>'Dessert')));
Both of which will return the following data:
//Data Returned
Array
(
0 => Array
{
[Recipe] => Array
(
[id] => 2745
[name] => Chocolate Frosted Sugar Bombs
[created] => 2007-05-01 10:31:01
[user_id] => 2346
)
[Tag] => Array
(
[0] => Array
(
[id] => 123
[name] => Breakfast
)
[1] => Array
(
[id] => 124
[name] => Dessert
)
[2] => Array
(
[id] => 125
[name] => Heart Disease
)
)
}
For more information on binding model associations on the fly see Creating and destroying associations on the fly
Mix and match techniques to achieve your specific objective.
3.7.6.6 Creating and Destroying Associations on the Fly
Sometimes it becomes necessary to create and destroy model associations on the fly. This may be for any number of reasons:
- You want to reduce the amount of associated data fetched, but all your associations are on the first level of recursion.
- You want to change the way an association is defined in order to sort or filter associated data.
This association creation and destruction is done using the CakePHP model bindModel() and unbindModel() methods. (There is also a very helpful behavior called "Containable", please refer to manual section about Built-in behaviors for more information). Let's set up a few models so we can see how bindModel() and unbindModel() work. We'll start with two models:
<?php
class Leader extends AppModel {
var $name = 'Leader';
var $hasMany = array(
'Follower' => array(
'className' => 'Follower',
'order' => 'Follower.rank'
)
);
}
?>
<?php
class Follower extends AppModel {
var $name = 'Follower';
}
?>
<?phpclass Leader extends AppModel {var $name = 'Leader';var $hasMany = array('Follower' => array('className' => 'Follower','order' => 'Follower.rank'));}?><?phpclass Follower extends AppModel {var $name = 'Follower';}?>
Now, in the LeadersController, we can use the find() method in the Leader model to fetch a Leader and its associated followers. As you can see above, the association array in the Leader model defines a "Leader hasMany Followers" relationship. For demonstration purposes, let's use unbindModel() to remove that association in a controller action.
function someAction() {
// This fetches Leaders, and their associated Followers
$this->Leader->findAll();
// Let's remove the hasMany...
$this->Leader->unbindModel(
array('hasMany' => array('Follower'))
);
// Now a using a find function will return
// Leaders, with no Followers
$this->Leader->findAll();
// NOTE: unbindModel only affects the very next
// find function. An additional find call will use
// the configured association information.
// We've already used findAll() after unbindModel(),
// so this will fetch Leaders with associated
// Followers once again...
$this->Leader->findAll();
}
function someAction() {// This fetches Leaders, and their associated Followers$this->Leader->findAll();// Let's remove the hasMany...$this->Leader->unbindModel(array('hasMany' => array('Follower')));// Now a using a find function will return// Leaders, with no Followers$this->Leader->findAll();// NOTE: unbindModel only affects the very next// find function. An additional find call will use// the configured association information.// We've already used findAll() after unbindModel(),// so this will fetch Leaders with associated// Followers once again...$this->Leader->findAll();}
Removing or adding associations using bind- and unbindModel() only works for the next model operation only unless the second parameter has been set to false. If the second parameter has been set to false, the bind remains in place for the remainder of the request.
Here’s the basic usage pattern for unbindModel():
$this->Model->unbindModel(
array('associationType' => array('associatedModelClassName'))
);
$this->Model->unbindModel(array('associationType' => array('associatedModelClassName')));
Now that we've successfully removed an association on the fly, let's add one. Our as-of-yet unprincipled Leader needs some associated Principles. The model file for our Principle model is bare, except for the var $name statement. Let's associate some Principles to our Leader on the fly (but remember–only for just the following find operation). This function appears in the LeadersController:
function anotherAction() {
// There is no Leader hasMany Principles in
// the leader.php model file, so a find here,
// only fetches Leaders.
$this->Leader->findAll();
// Let's use bindModel() to add a new association
// to the Leader model:
$this->Leader->bindModel(
array('hasMany' => array(
'Principle' => array(
'className' => 'Principle'
)
)
)
);
// Now that we're associated correctly,
// we can use a single find function to fetch
// Leaders with their associated principles:
$this->Leader->findAll();
}
function anotherAction() {// There is no Leader hasMany Principles in// the leader.php model file, so a find here,// only fetches Leaders.$this->Leader->findAll();// Let's use bindModel() to add a new association// to the Leader model:$this->Leader->bindModel(array('hasMany' => array('Principle' => array('className' => 'Principle'))));// Now that we're associated correctly,// we can use a single find function to fetch// Leaders with their associated principles:$this->Leader->findAll();}
There you have it. The basic usage for bindModel() is the encapsulation of a normal association array inside an array whose key is named after the type of association you are trying to create:
$this->Model->bindModel(
array('associationName' => array(
'associatedModelClassName' => array(
// normal association keys go here...
)
)
)
);
$this->Model->bindModel(array('associationName' => array('associatedModelClassName' => array(// normal association keys go here...))));
Even though the newly bound model doesn't need any sort of association definition in it’s model file, it will still need to be correctly keyed in order for the new association to work properly.
