【问题标题】:Doctrine - create One To Many with Join Table in YAMLDoctrine - 在 YAML 中使用连接表创建一对多
【发布时间】:2012-01-12 06:38:42
【问题描述】:

我正在尝试使用连接表在用户和角色之间创建一对多关系。 我还没有设法在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: {  }

【问题讨论】:

  • 是否正在尝试将相同的关系从 SF1 迁移到 SF2?

标签: symfony1 doctrine yaml


【解决方案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: {  }

【讨论】:

  • 另外,看起来你是在做多对多,而不是单对多
猜你喜欢
  • 2016-01-09
  • 2011-03-10
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2022-03-08
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多