2012-11-27 128 views
7

我有2个实体,即匹配和团队。一个团队可以有一对多的匹配。但是,我的匹配实体const的2个字段引用同一个实体Team。他们是$ homeTeam和$ awayTeam。我如何参考Team,$ matches中的同一个字段作为双向关系?Doctrine2映射:2个字段映射到一个字段(ManyToOne)

我现在不工作的代码如下:

我的比赛实体:

/** 
* @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; 

我的团队实体(不正确我会假设):

/** 
* @ORM\Entity 
* @ORM\Table(name="team") 
* **/ 
class Team { 

    /** @ORM\OneToMany(targetEntity="Match", mappedBy="homeTeam", mappedBy="awayTeam") **/ 
    protected $matches; 
+0

我有同样的问题,但你需要加入与OR条件:homeTeam或AwayTeam,作为我来说,我需要和条件加盟。 – Dmitriy

回答

7

探索Doctrine's official docs后:您不能添加多个mappedBy列。取而代之的是,你可以选择之间:

  1. Match创建一个自定义库,定义方法getAllMatchesForTeam($team)
  2. 确定适当的关系$homeMatchesTeam$awayMatches +方法getAllMatches()$homeMatches工会结果和$awayMatches

更多在这里阅读:

  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

谢谢德米特里 – Blyde