Doctrine2多对一自我引用:未生成父对象

时间:2012-11-01 08:35:26

标签: symfony doctrine-orm self-reference

我正在使用带有学说的Symfony2,并且除了一对多/一对多的自引用关系之外没有关系问题。

我有一个实体客户可以拥有零个,一个或多个实体(也是客户)。当我使用'doctrine:generate:entities BundleName'生成实体时,我的Entity Customer.php中只有一个var'$ entities',并且没有var'$ mother_house'。生成的迁移(使用doctrine:migrations:diff)也不包含创建新字段'mother_house_id'。

Customer.orm.yml中的架构是这样的:

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

1 个答案:

答案 0 :(得分:1)

我发现了我的错误。

所有相同类型的关系(多对一,一对多等)必须归入一个字段“多个一个”,“一个多个”等等。

所以我只需要改变

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

进入

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