我有 2 个实体,即比赛和团队。一支球队可以进行一对多的比赛。但是,我的 Match 实体由 2 个字段组成,它们引用同一实体 Team。他们是 $homeTeam 和 $awayTeam。如何将团队中的同一字段 $matches 引用为双向关系?
我当前的非工作代码如下:
我的匹配实体:
/**
* @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;
经过探索Doctrine 的官方文档 http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html: 不能添加多个mappedBy
列。您还可以选择:
- 创建自定义存储库
Match
并定义方法getAllMatchesForTeam($team)
- 定义适当的关系
$homeMatches
and $awayMatches
+ 方法getAllMatches()
on Team
和联合结果$homeMatches
and $awayMatches
there
在这里阅读更多内容:
- https://stackoverflow.com/questions/13922047/symfony2-doctrine2-how-to-implement-methods-on-entity-to-retrieve-lated-ent https://stackoverflow.com/questions/13922047/symfony2-doctrine2-how-to-implement-methods-on-entity-to-retrieve-related-ent
- Symfony2 中的自定义存储库类 https://stackoverflow.com/a/8148065/541961
- 通过 Twig 扩展中的自定义存储库获取数据 https://stackoverflow.com/questions/8450465/fetching-data-through-a-custom-repository-in-a-twig-extension
- 如何使用 Symfony2 访问控制器外部的服务? https://stackoverflow.com/questions/6124444/how-can-i-access-a-service-outside-of-a-controller-with-symfony2/6127711#6127711
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)