symfony2中有多个多对多的关系

时间:2012-03-01 22:25:27

标签: symfony doctrine-orm relational-database

我有3个实体:

  • 艺术家
  • 类型

一首歌可以有多种类型,反之亦然。一首歌也可以有多位艺术家,反之亦然。

这就是为什么我要创建两个多对多关系的原因。但是,在生成Entity clases时,第二个中的第一个总是被第二个“覆盖”,并且没有为该那个创建变量或访问器。

这是我的名字:(已更新)

Foo\BarBundle\Entity\Song:
  type: entity
  table: song
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    title:
      type: string
      length: 300
    mime_type:
      type: string
      length: 20
    filesize:
      type: integer
      length: 20
    length:
      type: string
      length: 20
    created_at:
      type: datetime
      gedmo:
        timestampable:
          on: create
  manyToOne:
    user:
      targetEntity: Spotaset\UserBundle\Entity\User
      inversedBy: songs
  manyToMany:
    genres:
      targetEntity: Genre
      inversedBy: songs
  manyToMany:
    artists:
      targetEntity: Artist
      inversedBy: songs


Foo\BarBundle\Entity\Artist:
  type: entity
  table: artist
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 100
      unique: true
  manyToMany:
    songs:
      targetEntity: Song
      mappedBy: artistss

Foo\BarBundle\Entity\Genre:
  type: entity
  table: genre
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 100
      unique: true
  manyToMany:
    songs:
      targetEntity: Song
      mappedBy: genres

我对关系数据库知之甚少,所以我不知道这是一个错误还是这个设计一般都不好......

非常感谢所有帮助!

1 个答案:

答案 0 :(得分:9)

最后解决了这个(愚蠢的)问题。原来我必须在一个很多ToTanyMany标题下定义多对多:

  manyToMany:
    genres:
      targetEntity: Genre
      inversedBy: songs
    artists:
      targetEntity: Artist
      inversedBy: songs
相关问题