There are 4 Relationship / Associations types in CakePHP:
Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple post.
many to one belongsTo Many post belong to a user.
many to many hasAndBelongsToMany Post have, and belong to many tags.
CakePHP relationship types examples.
Eg: hasOne-> A user has one profile
class User extends AppModel
{
public $hasOne = 'Profile';
}
Example: hasMany-> A user can have multiple post.
class User extends AppModel {
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'conditions' => array('Post.approved' => '1'),
'order' => 'Recipe.created DESC'
)
);
}
Example: belongsTo -> Many post belong to a user.
class Post extends AppModel {
public $belongsTo = array(
'User' => array(
'className' => 'User',
)
);
}
0 Comment(s)