我有以下数据库关系:
Building hasMany Shape
Appartment hasOne Shape
Building hasMany Appartment
Shape belongsTo Building & Appartment
现在,在Shape模型中,我想要检索属于某个建筑物的所有形状。这包括属于该特定建筑物的公寓的形状。
E.g。
Building 'X'
There are 4 shapes directly linked to building X via the belongsTo Building relation.
Shape->Building
And there are 2 shapes directly linked to 2 appartments via the belongsTo Appartment relation. These 2 appartments again are directly linked to Building X via the belongsTo Building relation. Thus these 2 shapes are indirectly linked to building X.
Shape->Appartment->Building
我似乎无法理解这一点,非常感谢帮助。
答案 0 :(得分:2)
答案 1 :(得分:0)
在您的查找中提升recursive value,尝试一下。
答案 2 :(得分:0)
您可能还想重新考虑使用shape作为自己的表。
例如,如果您的表只有一个正确,并且如下所示:
shapes
id int
shape varchar 50
而是这样做:
buildings
id int
bulding_number int
shape varchar 50
...
appartments
id int
appartment_number int
shape varchar 50
...
在建筑物和公寓桌上都可以更容易地选择形状。您的数据库将更简单,更快速,并且编程可能更容易。
但是,如果您想要捕捉的其他形状属性就在前方。