开发者

Problem with symfony2 schema yaml: unable to get relationships working

开发者 https://www.devze.com 2023-04-07 16:26 出处:网络
I have two entities: User and Student. Following is the yaml schema: ABC\\UserBundle\\Entity\\User: type: entity

I have two entities: User and Student. Following is the yaml schema:

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

My problem is that when I do "doctine:update:schema --dump-sql", user_id field is not added to the Student table and no relationship is created between the entities.


Yaml map is case-sensitive, use the proper name oneToOne for creating relations.


Your are missing the joinColumn option for the OneToOne association:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号