Doctrine Entity缺少已分配的ID

时间:2012-04-06 22:00:29

标签: symfony doctrine-orm

我有两个实体,联系人和全球。

Example\EventBundle\Entity\EventGlobal:
    type: entity
    table: EventGlobal
       fields:
          id:
              id: true
              type: integer
              generator:
                strategy: AUTO
  oneToOne:
      EventKontakt:
      targetEntity: Example\EventBundle\Entity\EventContact
      mappedBy: EventGlobal
      cascade: ["persist"]



Example\EventBundle\Entity\EventContact:
    type: entity
    table: EventContact
    fields:
      EventGlobal_id:
       id: true
       type: integer
      oneToOne:
        EventGlobal:
         targetEntity: Example\EventBundle\Entity\EventGlobal
         inversedBy: EventContact
         joinColumns:
           EventGlobal_id:
             referencedColumnName: id
             nullable: false

他们工作正常。现在我用Symfony构建一个Form

        public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add(Stuff..)
        ->add('EventContact', new EventContactType($this->options))
    ;
}

表单已正确呈现,具有一对一的关系。 但是当我保存时,我得到了一个错误。

我的错误是:

    Entity of type Example\EventBundle\Entity\EventContact is missing an assigned ID. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly. 

我如何能够通过Symfony / Doctrine完成我的安检?

1 个答案:

答案 0 :(得分:0)

您需要为EventContact添加生成器。

用于您的EventContact实体。

fields:
   EventGlobal_id:
   id: true
   type: integer
   generator:
       strategy: AUTO