Ivo Necchi
Ivo Necchi

Reputation: 43

Symfony/Doctrine2/YAML missing maps in generated PHP class

I'm trying to create these three classes using Doctrine 2 + Symfony, with bi-direcional YAML mappings.

Everything works fine, without errors, but the generated entity contains only the two first collection declarations for the User entity. The entity also does not have the add, remove and get functions for the missing link.

I tryed to generate each mapping separately, works fine.

Is that a limitation of Doctrine2?

Generated MyBundle\Entity\User.php constructor (ArrayCollection for Token class is missing):

public function __construct()
{
    $this->owned = new \Doctrine\Common\Collections\ArrayCollection();
    $this->shared = new \Doctrine\Common\Collections\ArrayCollection();
}

Collection.orm.yml:

MyBundle\Entity\Collection:
  type: entity
  table: collection
  id:
    collection_id:
      type: integer
      generator:
        strategy: AUTO 
  fields:
    ...
  manyToOne:
    owner: 
      targetEntity: User
      inversedBy: owned
      joinColumn:
        name: user_id
        referencedColumnName: user_id    
  manyToMany:
    shares:
      targetEntity: User
      inversedBy: shared
      joinTable:
        name: shares
        joinColumns:
          collection_id:
            referencedColumnName: collection_id
        inverseJoinColumns:
          user_id:
            referencedColumnName: user_id

Token.orm.yml:

MyBundle\Entity\Token:
  type: entity
  table: token
  id:
    token_id:
      type: integer
      generator:
        strategy: AUTO 
  fields:
    ...
  manyToOne:
    user: 
      targetEntity: User
      inversedBy: tokens
      joinColumn:
        name: user_id
        referencedColumnName: user_id

User.orm.yml:

MyBundle\Entity\User:
  type: entity
  table: user
  id:
    user_id:
      type: integer
      generator:
        strategy: AUTO 
  fields:
    ...
  oneToMany:
    owned:
      targetEntity: Collection
      mappedBy: owner
  oneToMany:
    tokens:
      targetEntity: Token
      mappedBy: user
  manyToMany:
    shared:
      targetEntity: Collection
      mappedBy: shares

Upvotes: 1

Views: 67

Answers (1)

ClickLabs
ClickLabs

Reputation: 557

Yaml does not handle duplicated keys. oneToMany should be defined once.

MyBundle\Entity\User:
  type: entity
  table: user
  id:
    user_id:
      type: integer
      generator:
        strategy: AUTO 
  fields:
    ...
  oneToMany:
    owned:
      targetEntity: Collection
      mappedBy: owner
    tokens:
      targetEntity: Token
      mappedBy: user
  manyToMany:
    shared:
      targetEntity: Collection
      mappedBy: shares

Upvotes: 1

Related Questions