Eti*_*oël 5 php one-to-many many-to-one symfony doctrine-orm
我们先来描述一下我的情况.我正在使用Symfony2,我的实体之间的关系有问题.
我有两个链接在一起的实体.这两个实体是AssociationQuestion
和AssociationPossibleAnswer
.我目前正在创建一个问题软件,其中必须将左侧的一个可能答案与右侧的另一个可能答案相关联,例如在以下示例中:
目前,我正计划在类中使用两个属性来AssociationQuestion
保存许多AssociationPossibleAnswer
对象.第一个数组将包含左侧可能的答案,第二个数组将包含右侧可能的答案.
因此,对我来说,看起来我将有两个oneToMany关系 AssociationQuestion
AssociationQuestion:
oneToMany:
possibleAnswersLeft:
targetEntity: AssociationPossibleAnswer
mappedBy: associationQuestion
possibleAnswersRight:
targetEntity: AssociationPossibleAnswer
mappedBy: associationQuestion
Run Code Online (Sandbox Code Playgroud)
然后,在AssociationPossibleAnswer
,我会有一个ManyToOne关系:
AssociationPossibleAnswer:
manyToOne:
associationQuestion:
targetEntity: AssociationQuestion
Run Code Online (Sandbox Code Playgroud)
问题是我在尝试验证我的学说时遇到以下错误.似乎你不能像我希望的那样将两个实体链接到一个......
* The field AssociationQuestion#possibleAnswersLeft is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity AssociationPossibleAnswer#associationQuestion does not contain the required 'inversedBy=possibleAnswersLeft' attribute.
* The field AssociationQuestion#possibleAnswersRight is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity AssociationPossibleAnswer#associationQuestion does not contain the required 'inversedBy=possibleAnswersRight' attribute.
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是在我的两个实体之间建立关系的正确方法.是否有可能有两个属性指向实体,而实体却不知道它指向哪个属性.
使用OneToMany关联无法解决此案例.
你需要之间的2个不同的关系AssociationQuestion
和AssociationPossibleAnswer
.你设置它的方式,你只有1个关系.只需看看您创建的1 ManyToOne关联AssociationPossibleAnswer
.
并且你试图在这个关系中有两个相反的方面,这在理论上是不可能的.关系只能有2个端点(不是3个).
解
实现2(单向)ManyToMany关联AssociationQuestion
,并使外键指向AssociationPossibleAnswer
唯一:
class AssociationQuestion
{
/**
* @ORM\ManyToMany(targetEntity="AssociationPossibleAnswer")
* @ORM\JoinTable(name="association_question_association_possible_answer_left",
* joinColumns={@ORM\JoinColumn(name="association_question_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="association_possible_answer_id", referencedColumnName="id", unique=true)}
* )
*/
private $possibleAnswersLeft;
/**
* @ORM\ManyToMany(targetEntity="AssociationPossibleAnswer")
* @ORM\JoinTable(name="association_question_association_possible_answer_right",
* joinColumns={@ORM\JoinColumn(name="association_question_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="association_possible_answer_id", referencedColumnName="id", unique=true)}
* )
*/
private $possibleAnswersRight;
// ...
Run Code Online (Sandbox Code Playgroud)
Doctrine称这是一对多,单向与联接表关联.
归档时间: |
|
查看次数: |
2448 次 |
最近记录: |