2013-03-21 66 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