mysql - Symfony2 多对多关系数据库结构

标签 mysql database symfony orm doctrine-orm

我正在使用 Symfony2 构建一个用于管理电影数据库的应用程序。 我有电影表和艺术家表。

现在在电影中可能有许多艺术家扮演不同的角色,我的问题就开始了,因为我不太擅长关系映射,所以我面临着问题。

对于电影 X 艺术家 A 是 Actor ,对于电影 Y 艺术家 B 是 Actor 和导演。

现在我的问题如何构建连接表,以便我可以保存检索艺术家相关的作品,例如:电影名称、角色(可能是一部电影的多个角色)

到目前为止我的 ORM 是:电影

table: content
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    title:
        type: string
        length: 120
        unique: true
manyToMany:
    actor:
          targetEntity: Artist
          joinTable:
              name: content_actor
              joinColumns:
                   content_id:
                        referencedColumnName: id
              inverseJoinColumns:
                   artist_id:
                        referencedColumnName: id

艺术家的 ORM:

 id:
        id:
        type: integer
        generator: { strategy: AUTO }
fields:
    name:
        type: string
        length: 255
        unique: true

但是这会产生问题,对于导演,我为歌手、摄影师、女 Actor 等创建了另一个连接表,那么如何才能有一个表来存储所有信息并标准化数据呢?

感谢帮助

最佳答案

好吧,我用另一种方式写:

使用可以使用这个映射:

电影:

Movie:
    type: entity
    table: movie
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title: ~
        createdAt:
            type: datetime
    oneToMany:
        roles:
            targetEntity: Role
            mappedBy: movie

艺术家:

Artist:
    type: entity
    table: artist
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        fname: ~
        lname: ~
        birthDate:
            type: datetime
        #other fields for this entity

角色:

Role:
    type: entity
    table: role
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name: ~

    manyToMany:
        artist:
            targetEntity: Artist

    manyToOne:
        movie:
            targetEntity: Movie
            inversedBy: roles

关于mysql - Symfony2 多对多关系数据库结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29692163/

相关文章:

php - 瓢虫调试器的一些功能在 symfony2 php 应用程序中不起作用

php - Symfony2 将选定的日期从表单保存到数据库作为 DateTime

php - 为什么我的 MySQL INSERT 语句返回错误? PDOException SQLSTATE[42000] :

MySQL:如果表 B 中存在记录,则返回表 A 的所有行和 true|false

java - 将Java连接到MySQL数据库

php - "MySQL server has gone away"错误,我觉得不是超时

mysql - 使用 substring_index MySQL 获取带有破折号的名称

mysql - '' mysql中单引号语法错误,但查询中没有单引号

php - 查询失败 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法

php - DataFixture 不使用 nelmio/alice 保存(带有 flex 的 sf 3.3)