学说 - 在YAML中使用连接表创建一对多

时间:2011-12-03 18:10:51

标签: symfony1 doctrine yaml

我尝试使用连接表在用户和角色之间创建一对多关系。 我没有能够在doctrine docs

中找到YAML示例

我如何宣布与YAML的等效关系?

/**
 * @ORM\ManyToMany(targetEntity="Role")
 * @ORM\JoinTable(name="user_role",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 *
 * @var ArrayCollection $userRoles
 */
protected $userRoles;

YAML文件:

Acme\AcmeBundle\User:
  type: entity
  table: null
  fields:
  id:
    type: integer
    id: true
    generator:
    strategy: AUTO
  forename:
    type: string
    length: 255
  surname:
    type: string
    length: 255
  email:
    type: string
    length: 255
  lifecycleCallbacks: {  }

Acme\AcmeBundle\Role:      
  type: entity
  table: null
  fields:
  id:
    type: integer
    id: true
    generator:
    strategy: AUTO
  name:
    type: string
    length: 255
  createdAt:
    type: datetime
  lifecycleCallbacks: {  }

1 个答案:

答案 0 :(得分:1)

这是你要找的吗?

Acme\AcmeBundle\User:
  type: entity
  manyToMany:
      roles:
          targetEntity: Role
          joinTable:
              name: user_role
              joinColumns:
                  user_id:
                      referencedColumnName: id
              inverseJoinColumns:
                  role:
                      referencedColumnName: id
  table: null
  fields:
  id:
    type: integer
    id: true
    generator:
    strategy: AUTO
  forename:
    type: string
    length: 255
  surname:
    type: string
    length: 255
  email:
    type: string
    length: 255
  lifecycleCallbacks: {  }