Samia Ruponti
Samia Ruponti

Reputation: 4038

Symfony2 Many to many relational Db Structure

I am using Symfony2 to build one app for managing a movie database. I have Movie table and Artist Table.

now In movie there could many artist with different role and there my problem starts, as i am not very good at relational mapping i am facing problem.

For movie X Artist A is Actor, For Movie Y Artist B is Actor and Director.

Now my question How can I build the Joining table so that I can save retrieve Artists related works like : Movie name, Role(could be multiple role for one movie)

So far my ORM is: Movie

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 for Artist:

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

But this create issue that for director I have create another joining table for singer, camera man, Actress etc so how can just have one table where i can store all info and normalize the data?

Thanks for help

Upvotes: 0

Views: 127

Answers (1)

ghanbari
ghanbari

Reputation: 1629

ok, i write another way:

use can use this mapping:

Movie:

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

Artist:

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

Role:

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

    manyToMany:
        artist:
            targetEntity: Artist

    manyToOne:
        movie:
            targetEntity: Movie
            inversedBy: roles

Upvotes: 1

Related Questions