繁体   English   中英

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

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

我对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: {  }

如您所见,在一个User实体上指向两个oneToOne字段。问题是,如果我要求Doctrine给我这些论文oneToOne,那么这两种方法都不会起作用。

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

在多个关系中定义实体绝对好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM