2011-09-25 41 views
0

我有兩個實體:用戶和學生。以下是YAML架構:symfony2模式問題yaml:無法讓關係工作

ABC\UserBundle\Entity\User: 
    type: entity 
    table: abc_user 
    id: 
    id:  { type: integer, generator: { strategy: AUTO } } 
    fields:   
    username: { type: string, length: 255, notnull: true, unique: true } 
    email: { type: string, length: 255, notnull: true, unique: true } 
    password: { type: string, length: 255, notnull: true } 
    enabled: { type: boolean } 


ABC\UserBundle\Entity\Student: 
    type: entity 
    table: abc_student 
    id: 
    id: { type: integer, generator: { strategy: AUTO } }   
    fields: 
    first_name: { type: string, length: 255, notnull: true } 
    middle_name: { type: string, length: 255 } 
    last_name: { type: string, length: 255, notnull: true } 
    OnetoOne: 
    user: 
     targetEntity: ABC\UserBundle\Entity\User 

我的問題是,當我做「doctine:更新時間:架構自卸SQL」,USER_ID字段不添加到學生表中,並在實體之間建立任何關係。

回答

0

你缺少該OneToOne協會joinColumn選項:

ABC\UserBundle\Entity\User: 
    type: entity 
    table: abc_user 
    id: 
    id:  { type: integer, generator: { strategy: AUTO } } 
    fields:   
    username: { type: string, length: 255, notnull: true, unique: true } 
    email: { type: string, length: 255, notnull: true, unique: true } 
    password: { type: string, length: 255, notnull: true } 
    enabled: { type: boolean } 
    OnetoOne: 
    student: 
     targetEntity: ABC\UserBundle\Entity\Student 
     mappedBy: user 

ABC\UserBundle\Entity\Student: 
    type: entity 
    table: abc_student 
    id: 
    id: { type: integer, generator: { strategy: AUTO } }   
    fields: 
    first_name: { type: string, length: 255, notnull: true } 
    middle_name: { type: string, length: 255 } 
    last_name: { type: string, length: 255, notnull: true } 
    OnetoOne: 
    user: 
     targetEntity: ABC\UserBundle\Entity\User 
     joinColumn: 
     name: user_id 
     referencedColumnName: id 
+0

joinColumn是可選的 – meze

+0

謝謝。我想要一個一對一的單向映射,這就是爲什麼我只把關係放在Student實體中。此外,它應該默認選擇name ='user_id'和referencedColumnName ='id'。我儘早試過,但沒有幫助。我今天晚上再次放棄,看看我能不能找到任何地方。 –

+0

是的,joinColumn是可選的,所以無視我的答案。這裏真正的問題是** OnetoOne **的大小寫,應該是** oneToOne **,如另一個答案中所述。 –

1

YAML地圖是大小寫敏感的,使用正確的名稱oneToOne創建關係。

+0

謝謝。也許,我從Doctrine文檔頁面的註釋示例中找到了「OneToOne」。我會嘗試'onetoOne',看看它是否有幫助。 –

+0

將'OneToOne'更改爲'oneToOne'沒有任何幫助。我也嘗試使用Doctrine \ ORM \ Tools \ SchemaValidator類驗證模式,但它不返回任何錯誤。 –

+0

我使用doctrine:mapping:convert命令將我的模式從yaml轉換爲xml,在xml中添加了一對一映射並且它可以工作。 雖然,仍然不確定原始問題是由於我的錯誤,還是symfony/doctrine中的錯誤。 –