[英]CakePHP: hasMany with join table
I have two models: Store
and Review
. 我有两个模型:
Store
和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. 在我的数据库中,我有一个
reviews_stores
表reviews_stores
, reviews_stores
评论与商店相关联。
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
. 我假设一个
Store
应该有Many Review
,而一个Review
应该属于一个Store
,但是由于CakePHP假设我的reviews
表有一个名为store_id
的列,因此store_id
表会引起CakePHP问题。
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. 将创建一个新的
Review
记录,并将一条记录插入到brands_reviews
表中以brands_reviews
两个记录关联起来。
Any help would be much appreciated. 任何帮助将非常感激。
Why are you not simply using one Review model and a reviews table with a field "foreign_key" and another field "model"? 为什么不简单地使用一个带有字段“ foreign_key”和另一个字段“ 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. 如果要继续使用数据库设计,则必须在hasMany关联上使用HABTM关联。 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.
但是,即使您想保持该连接性,也可以使用foreign_key / model,并且只具有一个联接表和一个评论表。
By the way, your join table review_store does not follow the conventions, it should be reviews_stores. 顺便说一句,您的联接表review_store不遵循约定,应该为reviews_stores。 But then it differs to the schema you've used for brands_reviews.
但这不同于您用于brand_reviews的架构。 ;)
;)
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.
id失去了联接表,只剩下一个额外的表,概述了评论所属的“组”。 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).
因此,评论表将具有review_id,link_id(相关品牌或商店的外键),review_type_id(描述评论是针对品牌还是商店的外键等)。 Then the review_type table only needs to have review_type_id, review_type(varchar).
然后review_type表仅需要具有review_type_id,review_type(varchar)。
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. 无需为每个模型都可以查看的所有联接表,只需将模型名称本身存储在
Review
表的字段中即可。
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. 然后,您可以根据需要使用任意数量的额外模型来执行此操作,而无需接触数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.