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 $homeTeam and $awayTeam. How do I reference the same field in Team, $matches, as a Bidirectional relationship?
My current non-working code is below:
My Match Entity:
/**
* @ORM\Entity
* @ORM\Table(name="match")
**/
class Match {
/**
* @ORM\ManyToOne(targetEntity="Team", inversedBy="matches")
* @ORM\JoinColumn(name="home_team_id", referencedColumnName="id")
* **/
protected $homeTeam;
/**
* @ORM\ManyToOne(targetEntity="Team", inversedBy="matches")
* @ORM\JoinColumn(name="away_team_id", referencedColumnName="id")
* **/
protected $awayTeam;
My Team Entity (incorrect I would presume?):
/**
* @ORM\Entity
* @ORM\Table(name="team")
* **/
class Team {
/** @ORM\OneToMany(targetEntity="Match", mappedBy="homeTeam", mappedBy="awayTeam") **/
protected $matches;
After exploring Doctrine's official docs: you can't add multiple mappedBy
columns. Instead of this, you can choose between:
Match
and define method getAllMatchesForTeam($team)
$homeMatches
and $awayMatches
+ method getAllMatches()
on Team
and union results of $homeMatches
and $awayMatches
thereRead more here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With