2013-03-21 45 views
1

我有两个实体:面试和评论。面试与评论有一对多的单向关系。学说2:无法正确设置一对多关系

这里是我的评论YAML映射文件:

Entities\Comment: 
    type: entity 
    table: Comment 
    repositoryClass: Repositories\CommentRepository 

    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    parentid: 
     type: integer 
     nullable: false 
     column: parentid 
    isactive: 
     type: integer 
     nullable: false 
     column: isactive 
    isremoved: 
     type: integer 
     nullable: false 
     column: isremoved 
    removaldate: 
     type: datetime 
     nullable: true 
     column: removaldate 
    user_name: 
     type: string 
     length: 255 
     nullable: false 
     column: user_name 
    user_email: 
     type: string 
     length: 255 
     nullable: false 
     column: user_email 
    user_avatar: 
     type: string 
     length: 255 
     nullable: false 
     column: user_avatar 
    comment: 
     type: text 
     nullable: false 
     column: comment 
    creationdate: 
     type: datetime 
     nullable: false 
     column: creationdate 
    rating: 
     type: integer 
     nullable: false 

这里是我的YAML映射文件专访:

Entities\Interview: 
    type: entity 
    table: Interview 
    repositoryClass: Repositories\InterviewRepository 

    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    isremoved: 
     type: integer 
     nullable: false 
     column: isremoved 
    removaldate: 
     type: datetime 
     nullable: true 
     column: removaldate 
    creationdate: 
     type: datetime 
     nullable: false 
     column: creationdate 
    rating: 
     type: integer 
     nullable: false 
    anonstitle: 
     type: string 
     length: 1000 
     nullable: false 
     column: anonstitle 
    anons: 
     type: text 
     nullable: false 
     column: anons 
    anonsphoto: 
     type: string 
     length: 255 
     nullable: true 
     column: anonsphoto 
    interviewtitle: 
     type: string 
     length: 1000 
     nullable: false 
     column: interviewtitle 
    interview: 
     type: text 
     nullable: true 
     column: interview 
    interviewphoto: 
     type: string 
     length: 255 
     nullable: true 
     column: interviewphoto 
    manyToMany: 
    comments: 
     targetEntity: Comment 
     joinTable: 
     name: interviews_comments 
     joinColumns: 
      interview_id: 
      referencedColumnName: id 
     inverseJoinColumns: 
      comment_id: 
      referencedColumnName: id 
      unique: true 

所以加载架构数据库后,我有3个表。其中2个是实体表,一个是关系,它只有2个列:interview_id,comment_id。但是在为某些面试保留Comment对象之后,我没有看到任何连接表。找不到原因。

+0

无论您遇到问题,请张贴控制器代码,更新或创建操作。 – Lighthart 2013-03-21 20:24:56

回答

0

面试与评论有一对多的单向关系。

没有,因为你在Entities\Interview定义这种关系为manyToMany,不是单向的oneToMany在持有端。

ManyToMany映射请求一个连接表。因为一个Interview可以有许多Comment和一个Comment可以有许多Interview。数据库表中的其他属性无法解决此问题,因此会创建其他映射表。

解决方案: 如果你想有一个Interview许多Comment,但一个Comment有一个Interview,你必须纠正YAML Entities\InterviewoneToMany映射没有joinTable(如您定义,它是创建) 。

+0

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table官方文档的这个页面显示一对多单向关系必须通过ManyToMany关键字定义。它创建连接表,但我不知道如何处理这些连接表的原因,当我添加评论或采访我有评论和采访表填充。但连接表中没有数据。 – 2013-03-25 04:11:55

+0

没有。 >单向一对多关联>>可以通过连接表来映射。 但不一定是。 – Athlan 2013-03-25 07:46:38

+0

好的,你说得对。但我如何操纵这些连接表?我必须自己填补他们或系统必须填写他们,而我添加评论或采访? – 2013-03-25 09:34:04