OneToOne双向自引用关系,第一列未创建

时间:2014-11-11 19:47:27

标签: symfony doctrine-orm one-to-one self-referencing-table

我试图创建1:1 bidirectionl自引用关系,如下所示:

class User extends AbstractUser implements UserInterface
{

    .....

    /**
     * @var User
     */
    private $binaryParent;

    /**
     * @var User
     */
    private $binaryChild;

    ....

    /**
     * @return User
     */
    public function getBinaryParent()
    {
        return $this->binaryParent;
    }

    /**
     * @param UserInterface $user
     * @return $this
     */
    public function setBinaryParent(UserInterface $user)
    {
        $this->binaryParent = $user;

        return $this;
    }

    /**
     * @return User
     */
    public function getBinaryChild()
    {
        return $this->binaryChild;
    }

    /**
     * @param UserInterface $user
     * @return $this
     */
    public function setBinaryChild(UserInterface $user)
    {
        $this->binaryChild = $user;

        return $this;
    }

    ....

}

这是我的xml映射:

...

<one-to-one field="binaryParent" target-entity="SL\CoreBundle\Entity\User" mapped-by="binaryChild">
    <join-column name="binary_parent_id" referenced-column-name="id" />
</one-to-one>

<one-to-one field="binaryChild" target-entity="SL\CoreBundle\Entity\User" inversed-by="binaryParent">
    <join-column name="binary_child_id" referenced-column-name="id" />
</one-to-one>

....

更新数据库后,仅创建binary_child_id,而binary_parent_id未创建。这里出了什么问题?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

两者都应该被反转,因为两者都是两个独立的1:1关系。只有拥有的一方(具有倒置)才会创建其列。