Vincent
Vincent

Reputation: 384

Doctrine2 Many-To-One self-referecing : parent not generated

I'm using Symfony2 with doctrine and have no problems with relations except for one many-to-one/one-to-many self-referencing relation.

I have an entity Customer that can have zero, one or more entities (which are also Customer). When I generate the entities using 'doctrine:generate:entities BundleName' I only have a var '$entities' in my Entity Customer.php and there is no var '$mother_house'. Also the generated migration (using doctrine:migrations:diff) does not contain the creation a new field 'mother_house_id'.

The schema in Customer.orm.yml is this one :

Acme\Bundle\CustomerBundle\Entity\Customer:
    type: entity
    table: customer
    repositoryClass: Acme\Bundle\CustomerBundle\Entity\CustomerRepository
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        company_name:
            type: string
            length: 255
        reference:
            type: string
            length: '20'
        created_at:
            type: datetime

    oneToMany:
        entities:
            targetEntity: Customer
            mappedBy: mother_house

    manyToOne:
        mother_house:
            targetEntity: Customer
            inversedBy: entities
            joinColumn:
                mother_house_id:
                    referencedColumnName: id

    manyToOne:
        created_by:
            targetEntity: Acme\Bundle\UserBundle\Entity\User
            joinColumn: 
                created_by:
                     referencedColumnName: id
    lifecycleCallbacks: {  }

Upvotes: 2

Views: 769

Answers (1)

Vincent
Vincent

Reputation: 384

I've found my mistake.

All relations of the same type (many-to-one, one-to-many, etc.) must be grouped under one field 'manyToOne', 'oneToMany', etc.

So I just had to change

manyToOne:
    mother_house:
        targetEntity: Customer
        inversedBy: entities
        joinColumn:
            mother_house_id:
                referencedColumnName: id

manyToOne:
    created_by:
        targetEntity: Acme\Bundle\UserBundle\Entity\User
        joinColumn: 
            created_by:
                referencedColumnName: id

into

manyToOne:
    mother_house:
        targetEntity: Customer
        inversedBy: entities
        joinColumn:
            mother_house_id:
                referencedColumnName: id
    created_by:
        targetEntity: Acme\Bundle\UserBundle\Entity\User
            joinColumn: 
            created_by:
                referencedColumnName: id

Upvotes: 1

Related Questions