Symfony2 如何在多对多连接中继承外键 table
Symfony2 How to inherit foreign keys inside a many to many join table
我有一个简单的结构,由三个 table 组成,一个 join table
Tournament
有一个 OneToMany
和 Group
。 Group
也有一个 ManyToMany
和 Team
。
这一切都很好也很简单,但是正如您在上图中看到的那样,Tournament
table,这对我很有帮助,因为我可以列出锦标赛中的所有球队,而无需遍历所有 Group
table.
那么,如何使用 symfony 2 和 doctrine
让这个字段出现在 jointable
中
谢谢
这是很常见的事情。它不能用 ManyToMany
关系来完成。相反,您需要 "join entity"、GroupTeamOwnership
.
该实体应具有一组、一个团队 ID 和一个锦标赛 ID。
因此,您将拥有:
而不是 ManyToMany
Group <-OneToMany-> GroupTeamOwnership <-ManyToOne-> Team
事实上,这种情况很常见 doctrine docs even mention it:
Why are many-to-many associations less common? Because frequently you
want to associate additional attributes with an association, in which
case you introduce an association class. Consequently, the direct
many-to-many association disappears and is replaced by
one-to-many/many-to-one associations between the 3 participating
classes.
我有一个简单的结构,由三个 table 组成,一个 join table
Tournament
有一个 OneToMany
和 Group
。 Group
也有一个 ManyToMany
和 Team
。
这一切都很好也很简单,但是正如您在上图中看到的那样,Tournament
table,这对我很有帮助,因为我可以列出锦标赛中的所有球队,而无需遍历所有 Group
table.
那么,如何使用 symfony 2 和 doctrine
让这个字段出现在jointable
中
谢谢
这是很常见的事情。它不能用 ManyToMany
关系来完成。相反,您需要 "join entity"、GroupTeamOwnership
.
该实体应具有一组、一个团队 ID 和一个锦标赛 ID。
因此,您将拥有:
而不是 ManyToManyGroup <-OneToMany-> GroupTeamOwnership <-ManyToOne-> Team
事实上,这种情况很常见 doctrine docs even mention it:
Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.