Reputation: 83
Whats the usage of creation relations like
var $belongsTo = array(
'UserType' => array(
'className' => 'UserType',
'foreignKey' => 'user_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasMany = array(
'UserOpenid' => array(
'className' => 'UserOpenid',
'foreignKey' => 'user_id',
'dependent' => true)
);
What if i added a table which created in the sense of "belongsTo", in "hasMany"? Any error occurs. How cake uses the relationships specified in the model?
Upvotes: 0
Views: 1104
Reputation: 522597
In addition to Paweł's answer:
"belongsTo", "hasMany" etc are simply terms chosen to reflect how the database is designed and how tables are related to each other, which determines how the data will be retrieved.
belongsTo
signifies a one-to-many relationship, with the "belonging" model being on the "many" sidehasMany
signifies the other side of a one-to-many relationshiphasOne
signifies a one-to-one relationshiphasAndBelongsToMany
signifies a many-to-many relationshipThe specific relationship determines which table holds which foreign key and how to JOIN
the tables to retrieve data automatically. Look into Normalization if you have no experience with this.
Upvotes: 0
Reputation: 3250
By associating models together you tell CakePHP what to get from the database when you use the find() function. Example:
posts BelongsTo users
= when you retrieve a post, you will also get data about user to whom this post belongs to (posts.user_id = users.id).
Upvotes: 1