处理条令实体上指向同一相关实体的两个oneToOne字段


Dealing with two oneToOne fields on Doctrine entity pointing on the same related entity

我对Symfony2(2.4-dev)和Doctrine2,有问题

我在MyObject.org.ml中定义了一个实体:

NS'ApiBundle'Entity'MyObject:
    type: entity
    table: my_objects
    indexes:
        moderator_id:
            columns:
                - moderator_id
        author_id:
            columns:
                - author_id
    id:
        id:
            id: true
            type: integer
            generator:
                strategy: IDENTITY
    oneToOne:
        author:
            targetEntity: User
            joinColumn:
                name: author_id
                referencedColumnName: id
    oneToOne:
        moderator:
            targetEntity: User
            joinColumn:
                name: moderator_id
                referencedColumnName: id
    fields:
        created:
            type: datetime
            nullable: true
        updated:
            type: datetime
            nullable: true
        published:
            type: datetime
            nullable: true
        title:
            type: string
            length: 255
            nullable: true
    lifecycleCallbacks: {  }

正如您所看到的,有两个oneToOne字段指向User实体问题是,如果我要求Doctrine给我一对一的论文,这对两者都不起作用。

在MyObject类中

$this->getAuthor()->getFullname(); // this work
$this->getModerator()->getFullname(); // this doesn't

错误为:错误:在非对象(…)上调用成员函数getFullname()

如果我删除作者oneToOne,那么getModerator()就会起作用,反之亦然。注意:指向的用户id有时可能与作者和主持人相同(这是个问题吗)

我是不是做错了什么?那是个虫子吗?

更改

oneToOne:
    author:
        targetEntity: User
        joinColumn:
            name: author_id
            referencedColumnName: id
oneToOne:
    moderator:
        targetEntity: User
        joinColumn:
            name: moderator_id
            referencedColumnName: id

oneToOne:
    author:
        targetEntity: User
        joinColumn:
            name: author_id
            referencedColumnName: id
    moderator:
        targetEntity: User
        joinColumn:
            name: moderator_id
            referencedColumnName: id

您只需要1个oneToOne部分

在多个关系中定义一个实体是完全可以的