两个属性与一个实体Symfony2共享OneToMany关系 [英] Two attributes sharing the same OneToMany relationship to one entity Symfony2

查看:92
本文介绍了两个属性与一个实体Symfony2共享OneToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们先来描述我的情况。我使用Symfony2,我的实体之间的关系有问题。



我有两个实体链接在一起。两个实体是 AssociationQuestion AssociationPossibleAnswer 。我目前正在创建一个问答软件,其中一个必须将左边的一个可能的答案链接到右边的另一个可能的答案,例如在下面的例子中:





目前,我计划有两个属性是类 AssociationQuestion 中的数组,它将包含许多 AssociationPossibleAnswer 对象。第一个数组将包含左边的可能答案,第二个数组将包含右边可能的答案。



因此,对我来说,看起来我会两个oneToMany关系在 AssociationQuestion

  AssociationQuestion:

oneToMany:
possibleAnswersLeft:
targetEntity:AssociationPossibleAnswer
mappedBy:associationQuestion

possibleAnswersRight:
targetEntity:AssociationPossibleAnswer
mappedBy:associationQuestion

然后,在 AssociationPossibleAnswer 中,我会有一个ManyToOne关系:

  AssociationPossibleAnswer:
manyToOne:
associationQuestion:
targetEntity:AssociationQuestion

问题是我收到以下错误,试图验证我的教义即似乎你不能有两个实体链接到一个,我想做...

  *字段AssociationQuestion #possibleAnswersLeft位于双向关系的反面,但目标实体AssociationPossibleAnswer#associationQuestion上指定的mappedBy关联不包含所需的inversedBy = possibleAnswersLeft属性。 

*字段AssociationQuestion#possibleAnswersRight位于双向关系的反面,但目标实体AssociationPossibleAnswer#associationQuestion上的指定的mappedBy关联不包含所需的inversedBy = possibleAnswersRight属性。

我想知道这是否是设置我的两个实体之间的关系的正确方法。 有可能有两个属性指向一个实体,而返回时实体不知道它指向哪个属性。

解决方案

此案例无法通过OneToMany关联解决。



您需要在 AssociationQuestion AssociationPossibleAnswer 。你设置的方式,你只有一个关系。只需看看在 AssociationPossibleAnswer 中创建的1 ManyToOne协会。



而您正试图拥有两个对面这个关系在理论上是不可能的。一个关系只能有2个终点(而不是3个)。



解决方案



AssociationQuestion 中实现2(单向)ManyToMany关联,并使外键指向 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将此调用为一个 - 多个,单向连接表关联。


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.

解决方案

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.

这篇关于两个属性与一个实体Symfony2共享OneToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆