简体   繁体   English

两个属性共享相同的 OneToMany 关系到一个实体 Symfony2

[英]Two attributes sharing the same OneToMany relationship to one entity Symfony2

Let's first describe my situation.先说一下我的情况。 I am using Symfony2 and I have a problem with a relationship between my entities.我正在使用 Symfony2 并且我的实体之间的关系有问题。

I have two entities that are linked together.我有两个连接在一起的实体。 The two entities are AssociationQuestion and AssociationPossibleAnswer .这两个实体是AssociationQuestionAssociationPossibleAnswer I am currently creating a questionary software where one would have to link one possible answer on the left to another one possible answer on the right, such as in the following example:我目前正在创建一个问题软件,其中必须将左侧的一个可能答案链接到右侧的另一个可能答案,例如在以下示例中:

Currently, I'm planning on having two attributes that are arrays in class AssociationQuestion that would hold many AssociationPossibleAnswer objects.目前,我计划拥有两个属性,它们是AssociationQuestion类中的数组,它们将包含许多AssociationPossibleAnswer对象。 The first array would contain the possible answers on the left and the second one would contain the possible answers on the right.第一个数组将包含左侧的可能答案,第二个数组将包含右侧的可能答案。

Therefore, to me, it looks like I would have two oneToMany relations in AssociationQuestion因此,对我来说,看起来我在AssociationQuestion有两个 oneToMany 关系

AssociationQuestion:

    oneToMany:
        possibleAnswersLeft:
            targetEntity: AssociationPossibleAnswer
            mappedBy: associationQuestion

        possibleAnswersRight:
            targetEntity: AssociationPossibleAnswer
            mappedBy: associationQuestion

Then, in AssociationPossibleAnswer , I would have one ManyToOne relation :然后,在AssociationPossibleAnswer ,我会有一个 ManyToOne 关系:

AssociationPossibleAnswer:
    manyToOne:
        associationQuestion:
            targetEntity: AssociationQuestion

The problem is that I get the following error trying to validate my doctrine.问题是我在尝试验证我的学说时遇到以下错误。 It seems that you can't have two entities linked to one as I would wish to do...似乎您不能像我希望的那样将两个实体链接到一个实体...

* 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.

I'm wondering if this is the proper way to set my relation between my two entities.我想知道这是否是设置我的两个实体之间关系的正确方法。 Is it possible to have two attributes pointing to an entity while in return the entity does not know which attribute it is being pointed from.是否有可能有两个属性指向一个实体,而作为回报,该实体不知道它是从哪个属性指向的。

This case cannot be solved with OneToMany associations.这种情况无法通过 OneToMany 关联解决。

You need 2 distinct relations between AssociationQuestion and AssociationPossibleAnswer .您需要AssociationQuestionAssociationPossibleAnswer之间的 2 个不同关系。 The way you've set it up, you only have 1 relation.按照您的设置方式,您只有 1 个关系。 Just look at the 1 ManyToOne association you created in AssociationPossibleAnswer .看看你在AssociationPossibleAnswer创建的 1 ManyToOne 关联。

And you're trying to have 2 opposite sides of that 1 relation, which is theoretically impossible.并且您正试图拥有 1 个关系的 2 个对立面,这在理论上是不可能的。 A relation can only have 2 endpoints (not 3).一个关系只能有 2 个端点(而不是 3 个)。

Solution解决方案

Implement 2 (unidirectional) ManyToMany associations in AssociationQuestion , and make the foreign key pointing to AssociationPossibleAnswer unique:AssociationQuestion实现 2 个(单向)ManyToMany 关联,并使指向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;

    // ...

Doctrine calls this a One-To-Many, Unidirectional with Join Table association. Doctrine 将此称为一对多、单向的连接表关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM