0

I have two models: Store and Review. Users of my site can leave a review on a store. In my database, I have a join table, reviews_stores that associates a review with a store.

How do I link my models? I assume a Store should haveMany Review, and a Review should belong to a Store, but the join table is causing issues with CakePHP as CakePHP is assuming my reviews table has a column called store_id.

The reason I'm using a join table is because many parts of my site can be reviewed. For example, brands. A new Review record will be created and a record will be inserted into a brands_reviews table to associate the two records.

Any help would be much appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
Martin Bean
  • 38,379
  • 25
  • 128
  • 201

3 Answers3

1

Why are you not simply using one Review model and a reviews table with a field "foreign_key" and another field "model"? By this you do not need to duplicate tables and inherit or duplicate models. This will also remove the need for a join table.

If you want to continue to use your db design then you'll have to use a HABTM association over the hasMany association. But even in the case you want to keep that jointable, again, you can use the foreign_key/model and simply have one join table and one reviews table.

By the way, your join table review_store does not follow the conventions, it should be reviews_stores. But then it differs to the schema you've used for brands_reviews. ;)

floriank
  • 25,546
  • 9
  • 42
  • 66
  • Because reviews can be associated with other models too, such as a brand as in my example above. The join table associates a review to a store, but it could be a brand or a sale or a blog post etc. – Martin Bean Apr 26 '12 at 01:45
  • 2
    Well, as I said, this is not necessary if you use two fields (foreign_key, model) and combine them. If you use uuids you do not even need the model field. The join table is *not* necessary. – floriank Apr 26 '12 at 08:30
0

Seems to me it isn't a many-many relationship but a grouped 1-many relationship. Id lose the join tables and simply have an extra table outlining which 'group' the review belongs to. So the review table would have review_id, link_id(the foreign key for the relevant brand or store), review_type_id(foreign key depicting whether the review is for a brand or store and so on). Then the review_type table only needs to have review_type_id, review_type(varchar).

mattwadey
  • 51
  • 4
  • So in the `review_type` table, would `review_type` be something like `Store` or `Brand`? – Martin Bean Apr 26 '12 at 08:47
  • That would be how I'd do it yeah. Then if you add more 'review_type's in future theyll just get a new idea. Effectively its a basic categorisation system for reviews similar to how a new categorisation system would work only that each review can have only 1 review type – mattwadey Apr 26 '12 at 09:37
  • Thanks, @mattwadey. Accepted your answer as you were helpful but more importantly, not an ass about things. – Martin Bean Apr 26 '12 at 10:23
  • @MartinBean How was I an ass about things? My answer reflects the structure that the Cake devs use to create their multi-model plugins. – Dunhamzzz Apr 26 '12 at 14:57
  • It wasn't aimed at you, @Dunhamzzz. – Martin Bean Apr 26 '12 at 16:49
0

There's no need for all the join tables for each model you can review, simple store the model name itself in a field in the Review table.

Setup your relationship like so:

class Store extends AppModel {
    public $hasMany = array(
        'Review' => array('conditions' => array('Review.model' => 'Store')
     );
}

You can then do this with as many extra models as you like without having to touch the database.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74