Search code examples
symfony1doctrineyaml

Doctrine - create One To Many with Join Table in YAML


Im trying to create a one To Many relationship between a User and a Role with a join table. I have not managed been able to find a YAML example in the doctrine docs

How would i declare an equivalent relationship with 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 files:

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: {  }

Solution

  • Is this what you're looking for?

    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: {  }