symfony2架构的问题yaml:无法使关系正常工作

时间:2011-09-25 15:10:20

标签: doctrine symfony

我有两个实体:用户和学生。以下是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:update:schema --dump-sql”时,user_id字段未添加到Student表中,并且实体之间没有创建任何关系。

2 个答案:

答案 0 :(得分:1)

Yaml映射区分大小写,使用正确的名称oneToOne来创建关系。

答案 1 :(得分: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