一对一,自我参照,双向关系

时间:2012-05-29 21:20:37

标签: php doctrine

我正在尝试使用实体Revision创建一对一,自引用,双向关系(),如下所示:< / p>

/**
 * @Entity()
 * @Table(name="rev")
 */
class Revision extends BaseEntity
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /**
     * @Column(type="string")
     */
    protected $comment;

    /**
     * @OneToOne(targetEntity="Revision", inversedBy="parent")
     * @JoinColumn(name="revision_of", referencedColumnName="id")
     */
    protected $revisionOf;

    /**
     * @OneToOne(targetEntity="Revision", mappedBy="revisionOf")
     */
    protected $parent;
}

没有parent属性,一切正常。当我添加该属性时,两个关系都返回null

当前的问题是:我可以在Doctrine中完成我想要做的事情吗?如果是这样,我将如何这样做?

2 个答案:

答案 0 :(得分:1)

我用这个解决了它

oneToOne:
  previous:
    targetEntity: Change
    mappedBy: next
    joinColumn:
      name: id
      referencedColumnName: next_id
  next:
    targetEntity: Change
    inversedBy: previous

答案 1 :(得分:0)

当你有双向关系时,你必须管理双方

在这种情况下

$revision->setRevisionOf($revisionOf);
$revisionOf->setParent($revision);

Doctrine Working with associations

In the case of bi-directional associations you have to update the fields on both sides: