doushi1957 2016-01-13 09:57
浏览 41
已采纳

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

I have a simple structure of three tables and a join table Tournament has a OneToMany with Group. Group also has a ManyToMany with Team.

enter image description here

this is all well and simple, but as you can see in the above image there is group_tournament_id key "inherited"(if that's the right term) from the Tournament table, this to me is very helpful as i can list all teams in the tournament, without looping through all the Group table.

so, how do i get this field to appear in the jointable using symfony 2 and doctrine

thanks

  • 写回答

1条回答 默认 最新

  • dongmi5607 2016-01-13 10:44
    关注

    This is a very common thing. It can not be done with ManyToMany relation. Instead, you need to have an "join entity", GroupTeamOwnership.

    That entity should have one group, one team id, and one tournament id.

    So instead having ManyToMany, you will have:

    Group <-OneToMany-> GroupTeamOwnership <-ManyToOne-> Team

    In fact, this is so common scenario that 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch