Two attributes sharing the same OneToMany relationship to one entity Symfony2

梦想与她 提交于 2019-12-07 05:11:05

问题


Let's first describe my situation. I am using Symfony2 and I have a problem with a relationship between my entities.

I have two entities that are linked together. The two entities are AssociationQuestion and AssociationPossibleAnswer. 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. 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:
        possibleAnswersLeft:
            targetEntity: AssociationPossibleAnswer
            mappedBy: associationQuestion

        possibleAnswersRight:
            targetEntity: AssociationPossibleAnswer
            mappedBy: associationQuestion

Then, in AssociationPossibleAnswer, I would have one ManyToOne relation :

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.


回答1:


This case cannot be solved with OneToMany associations.

You need 2 distinct relations between AssociationQuestion and AssociationPossibleAnswer. The way you've set it up, you only have 1 relation. Just look at the 1 ManyToOne association you created in AssociationPossibleAnswer.

And you're trying to have 2 opposite sides of that 1 relation, which is theoretically impossible. A relation can only have 2 endpoints (not 3).

Solution

Implement 2 (unidirectional) ManyToMany associations in AssociationQuestion, and make the foreign key pointing to AssociationPossibleAnswer unique:

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.



来源:https://stackoverflow.com/questions/21445505/two-attributes-sharing-the-same-onetomany-relationship-to-one-entity-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!