Association means relationship, It is a way that two table are connected to each other or linking model together, in cakephp the links between models are handled through association. User can understand it from the following table.
Relationship Association Type Example
one to one |
hasOne |
A user has one profile. |
one to many |
hasMany |
A user can have multiple blogs. |
many to one |
belongsTo |
Many blogs belong to a user. |
many to many |
hasAndBelongsToMany |
blogs have, and belong to, many ingredients. |
Source code in cake PHP:
class Movie extends AppModel {
var $name = 'Movie';
var $belongsTo = array( 'Director' => array( 'className' => 'Director' ) );
var $hasMany = array( 'Reviews' => array( 'className' => 'Review' ) );
// We don't need many settings in this association, because we'll stick to Cake's naming conventions
var $hasAndBelongsToMany = array( 'Genres' => array( 'className' => 'Genre' ) );
}
class Director extends AppModel {
var $name = 'Director' ;
$hasMany = array( 'Movie' => array( 'className' => 'Movie' ) );
}
class Review extends AppModel {
var $name = 'Review';
$belongsTo = array( 'Movie' => array( 'className' => 'Movie' ) );
}
class Genre extends AppModel {
var $name = 'Genre';
// We specify the join table here because Cake would expect the table to be called genres_movies from this side
$hasAndBelongsToMany = array( 'Movies' => array( 'className' => 'Movie',
'joinTable' => 'movies_genres'
);
}
The above line of code will help to understand association used in cake php model. Here are basic measure to used different table Movie have Id, Title and director_id while director table have Id, First_name and last_name.
0 Comment(s)