1

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

我在 MyObject.orm.yml 中定义了一个实体:

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 字段指向一个用户实体问题是,如果我要求 Doctrine 给我这些 oneToOne 它对两者都不起作用。

在 MyObject 类中

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

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

如果我删除作者 oneToOne,则 getModerator() 将起作用,反之亦然。注意:作者和版主指向的用户 ID 有时可能相同(这是一个问题)

我在这里做错了吗?那是一个错误吗?

4

1 回答 1

0

改变

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部分

在多个关系中定义一个实体绝对没问题

于 2013-06-24T15:40:18.580 回答