Thursday, March 26, 2009

Re: association type for model hasMany or HABTM

On Mar 26, 4:44 pm, programguru <ja...@w3developing.com> wrote:
> NOTE: CORRECTION IN MY PREVIOUS POST. I DID NOT MEAN TO SAY PRODUCTS - I WAS REFERRING TO CATEGORIES.

I'm not sure what you mean by this, what part of your original post
was erroneous?

You need HABTM whenever one of the two models can have multiple of the
other model, and the other model can belong to many of the first
model.

Your description of the situation in the first post does indicate you
will need HABTM. However, the schema in your second post indicates you
have: Gallery belongsTo Category AND a HABTM between Gallery and
Category. In most situations it's one or the other.

If a Gallery can belong to many Categories and a Category can have
many Galleries, then you need to remove the category_id from the
galleries table and create a join table for the HABTM relationship.
The join table will have two or three fields, depending on how you
prefer to setup your join tables (either category_id and gallery_id OR
id and category_id and gallery_id). Then in your Gallery model you
setup a hasAndBelongsToMany to Category and vice versa in the Category
model.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: