Doctrine2 Mapping: 2 fields mapped to one field (ManyToOne)

后端 未结 1 571
迷失自我
迷失自我 2021-02-15 14:33

I have 2 entities, namely Match and Team. A Team can have one to many Matches. However, my Match entity consts of 2 fields which reference the same entity, Team. They are $homeT

1条回答
  •  不知归路
    2021-02-15 14:48

    After exploring Doctrine's official docs: you can't add multiple mappedBy columns. Instead of this, you can choose between:

    1. Create a custom repository for Match and define method getAllMatchesForTeam($team)
    2. Define appropriate relations $homeMatches and $awayMatches + method getAllMatches() on Team and union results of $homeMatches and $awayMatches there

    Read more here:

    1. https://stackoverflow.com/questions/13922047/symfony2-doctrine2-how-to-implement-methods-on-entity-to-retrieve-related-ent
    2. Custom repository class in Symfony2
    3. Fetching data through a custom repository in a Twig extension
    4. How can I access a service outside of a controller with Symfony2?

    0 讨论(0)
提交回复
热议问题