tags:

views:

59

answers:

1

I have checked out the cookbook web site of cakephp that there are four types Model relationship:
http://book.cakephp.org/view/79/Relationship-Types

Since the one I am more familiar with is belongsTo,
I am not sure when I need to use hasManay and HABTM.
What will be the result to my web site if I used a wrong Model relationship type?
Please advise.

+2  A: 

belongsTo and hasMany are a pair and express a one-to-many relationship. One model belongs to another (i.e. it has a other_model_id field), while the other way around that other model has many records in the model that belongs to it. It's two sides of the same coin.

hasAndBelongsToMany is Cake's term for a many-to-many relationship. Here's a primer on this type of relationship. You use this when both models can have many of the other model, e.g. People-People friend relationships. A many-to-many relationship between two models entails three tables: model_a, model_b and model_a_model_b. If you're trying to use this type of relationship without the third table, you'll probably just get a bunch of errors.

deceze
This diagram may help you visualize the different types of relationships: http://mboffin.com/stuff/ruby-on-rails-data-relationships.png
deizel