symfony2:不能以一对一的关系合并形式插入

时间:2014-11-10 09:23:58

标签: php entity-framework symfony doctrine

客户实体与信贷实体具有一对一的关系。当我尝试插入时,我收到以下错误:

*

  

Jobeet \ JobBundle \ Entity \ Credit类型的实体缺少已分配的实体   字段ID' id'。此实体的标识符生成策略   需要在EntityManager#persist()之前填充ID字段   叫做。如果您想要自动生成标识符而不是您   需要相应地调整元数据映射。

*

我错过了什么?

Customer.orm.yml

type: entity
    table: customer
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
            nullable: true
        address:
            type: string
            length: 255

    oneToOne:
        credit:
            targetEntity: Credit
            mappedBy: customer
            cascade: [persist]

Credit.orm.yml

type: entity
table: credit
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    customer_id:
        type: integer
        unique: true
        nullable: false
    credit_name:
        type: string
        length: 255
        nullable: true
    credit_desc:
        type: string
        length: 255
        nullable: true

oneToOne:
    customer:
        targetEntity: Customer
        inversedBy: credit
        joinColumn:
            name: customer_id
            referencedColumnName: id

CustomerType.php

class CustomerType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('address')
            ->add('credit', new CreditType())
        ;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将 Credit.orm.yml 更改为

type: entity
    table: credit
    id:
        id:
            type: integer
            unique: true
            column: other_non_conflict_column_name
...