Symfony2许多到许多关系Db结构 [英] Symfony2 Many to many relational Db Structure
问题描述
我使用Symfony2构建一个应用程序来管理电影数据库。
我有电影表和艺术家表。
现在在电影中可能有许多艺术家不同的角色,我的问题开始,因为我不是很好关系映射我面临的问题。
电影X艺术家A是演员,电影Y艺术家B是演员和导演。
现在我的问题如何构建加入表,以便我可以保存检索Artists相关工作,如:电影名称,角色(可以是一个电影的多个角色) / p>
到目前为止,我的ORM是: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:
类型:整数
generator:{strategy:AUTO}
fields:
name:
type:string
length:255
unique:true
但是这个问题导致我为歌手,摄影师,女演员等创建了另一个联接表只有一个表格,我可以存储所有的信息和规范化数据?
感谢您的帮助
确定,我写另一种方式:
use可以使用这个映射:
电影:
电影:
类型:entity
表格:movie
id:
id:
type:integer
generator:{strategy:AUTO}
fields:
title:〜
createdAt:
type:datetime
oneToMany:
roles:
targetEntity:Role
mappedBy:movie
$ b b
艺术家:
艺术家:
类型:entity
表:artist
id:
id:
type:integer
generator:{strategy:AUTO}
fields:
fname:〜
lname:〜
birthDate:
type:datetime
此实体的其他字段
角色:
角色:
类型:entity
表:role
id:
id:
type:integer
generator:{strategy:AUTO}
fields:
name:〜
manyToMany:
artist:
targetEntity:Artist
manyToOne:
movie:
targetEntity:Movie
inversedBy:roles
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
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
这篇关于Symfony2许多到许多关系Db结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!