[英]Multiple relations to same table from different models
我希望有一种简单且可重用的方式来与具有不同模型的特定表共享关系。 我想做的是指定一个带有外键列(例如“ parent_id”)的表,然后将此表/模型与我的应用程序中的多个模型相关联。 有没有办法做到这一点,那是最好的方法? 我想我需要在此表中增加一列,以指定每行链接到的表,例如:
表格1
id 0 1 2名称name0 name1 name2 ...
表2
id 0 1 2名称name0 name1 name2 ...
Common_table:
id 0 1 ... parent_id 0 0 parent_table表1表2
我希望上面的解释有意义,在此先感谢!
是
创建关系时,您还可以设置条件
例如,在一个应用程序中,您有许多可以注释的模型,因此它们都与Comment
Model处于hasMany关系中。
您可以在注释表中创建一列,以存储要注释的模型的名称,并在parent_id
列中存储ID。
所以注释表通常是这样的
id | parent_id | model_name | comment_text | user_id
---+-----------+------------+----------------------+-------
1 | 15 | Post | I like this Post | 3
1 | 15 | Post | I like this Post too | 5
2 | 19 | Receipt | This receipt is good | 3
在您的Post
模型中,您可以执行
public hasMany =array(
'Comment' => array(
'foreignKey' => 'parent_id',
'conditions' => array('Comment.model_name' => 'Post')
)
在您的Receipt
模型中,您可以执行
public hasMany =array(
'Comment' => array(
'foreignKey' => 'parent_id',
'conditions' => array('Comment.model_name' => 'Receipt')
)
等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.