如何在单向多对多关系中创建额外的字段?

时间:2014-02-11 16:35:13

标签: symfony doctrine-orm

我发现了这个回复:https://stackoverflow.com/a/9135093/620095,但我不想使用新课程。我正在寻找最好的方式(优雅和直接)。

这是'标签'实体:

MyApp\CoreBundle\Entity\Tag:
    type: entity
    table: tag
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
            nullable: false

'发布'实体:

MyApp\PostBundle\Entity\Post:
    type: entity
    table: post
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
            length: 255
            nullable: false
        content:
            type: text
            nullable: false
    manyToMany:
        Tags:
            targetEntity: MyApp\CoreBundle\Entity\Tag
            joinTable:
                name: post_tag
                joinColumns:
                    post_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    tag_id:
                        referencedColumnName: id

'事件'实体:

MyApp\EventBundle\Entity\Event:
    type: entity
    table: event
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
            length: 255
            nullable: false
        content:
            type: text
            nullable: false
    manyToMany:
        Tags:
            targetEntity: MyApp\CoreBundle\Entity\Tag
            joinTable:
                name: event_tag
                joinColumns:
                    event_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    tag_id:
                        referencedColumnName: id

假设我想在'event_tag'表中找到'description'字段。我该怎么办?

更新:如果不可能,如何保留我的三个yml来添加引用的字段?

2 个答案:

答案 0 :(得分:0)

抱歉,您必须创建一个新实体。

您必须将此实体视为单个symfony实体

例如:

Product >- Stock -< Store
Product >- Bill -< User
...

而不是其他两个实体之间的联系。

答案 1 :(得分:0)

@cbacelar“我更新了这个问题。你能帮助我吗?” =&GT;事件 - 标签关系可能像:

在Event实体中,将manyToMany更改为oneToMany:

  oneToMany:
        EventTags:
            targetEntity: MyApp\EventBundle\Entity\EventTag
            mappedBy: Events

在Tag实体中,将manyToMany更改为oneToMany:

   oneToMany: 
        EventTags:
            targetEntity: MyApp\EventBundle\Entity\EventTag
            mappedBy: Tags

创建MyApp \ CoreBundle \ Entity \ EventTag实体:

type: entity
table: event_tag
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    description:
        type: text
        nullable: false
manyToOne:
    Events:
        targetEntity: MyApp\EventBundle\Entity\Event
        inversedBy: EventTags
        joinColumn:
           name: event_id
           referencedColumnName: id
    Tags:
        targetEntity: MyApp\CoreBundle\Entity\Tag
        inversedBy: EventTags
        joinColumn:
           name: tag_id
           referencedColumnName: id

`