gpt4 book ai didi

php - Symfony2 如何在多对多连接表中继承外键

转载 作者:行者123 更新时间:2023-11-30 22:27:37 25 4
gpt4 key购买 nike

我有一个简单的结构,包含三个表和一个连接表 Tournament 有一个OneToManyGroupGroup 还有一个 ManyToManyTeam

enter image description here

这一切都很好也很简单,但正如您在上图中所见,group_tournament_id 键“继承”(如果这是正确的术语)来自 Tournament表,这对我很有帮助,因为我可以列出锦标赛中的所有球队,而无需遍历所有 Group 表。

那么,我如何使用 symfony 2 和 doctrine 让这个字段出现在 jointable

谢谢

最佳答案

这是很常见的事情。用 ManyToMany 做不到关系。相反,您需要有一个“加入实体”,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.

关于php - Symfony2 如何在多对多连接表中继承外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34763500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com