[英]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.