【问题标题】:Failed on insert entity with many related entities插入具有许多相关实体的实体失败
【发布时间】:2018-05-01 00:40:29
【问题描述】:

这是我的第一篇文章,因为直到现在我一直通过你的答案找到解决方案,直到现在......

我有三个实体(自行车、类别和额外),它们有一些可翻译的字段。我知道 symfony 中有一个可翻译的扩展,但我发现有点晚了,我的解决方案一直在工作。

问题是我还有 3 个其他表(BikeI18N、CategoryI18N 和 ExtraI18N)。这些表链接与其关联的列bike_id(例如)。这里有 Bike 和 Category 的架构以及它们的 I18N 表:

自行车:

BEM\BikeBundle\Entity\Bike:
type: entity
table: null
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
    marca:
        type: string
        length: 255
    model:
        type: string
        length: 255
    preu1Dia:
        type: float
        column: preu_1dia
    [...]
manyToOne:
  category:
    targetEntity: BEM\CategoryBundle\Entity\Category
    inversedBy: bikes 
oneToMany:
  translation:
    targetEntity: BikeI18N
    mappedBy: bike
    cascade: [persist]        
  pedals:
    targetEntity: Pedal
    mappedBy: bike
  talles:
    targetEntity: Talla
    mappedBy: bike
manyToMany:
  accessoris:
    targetEntity: Accessori
    inversedBy: bikes      
lifecycleCallbacks: {  }

BikeI18N:

BEM\BikeBundle\Entity\BikeI18N:
type: entity
table: null
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
    descripcio:
        type: text
    locale:
        type: string
        length: '2'
manyToOne:
  bike:
    targetEntity: Bike
    inversedBy: translation  
    joinColumn:
      bike_id:
        referencedColumnName: id        
lifecycleCallbacks: {  }

类别:

BEM\CategoryBundle\Entity\Category:
type: entity
table: null
repositoryClass: BEM\CategoryBundle\Repository\CategoryRepository
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
oneToMany:
  translation:
    targetEntity: CategoryI18N
    mappedBy: category
    cascade: [persist]
  bikes:
    targetEntity: BEM\BikeBundle\Entity\Bike
    mappedBy: category
lifecycleCallbacks: {  }

最后是 CategoryI18N:

BEM\CategoryBundle\Entity\CategoryI18N:
type: entity
table: null
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
    categoryId:
        type: integer
        column: category_id
    locale:
        type: string
        length: '2'
    translation:
        type: string
        length: 255
manyToOne:
  category:
    targetEntity: Category
    inversedBy: translation
    joinColumn:
      category_id:
        referencedColumnName: id        
lifecycleCallbacks: {  }

在我的“新”表单中,我有一个包含 4 个嵌入的 CategoryI18N 表单的 Category 表单。它工作正常。问题是即使我做了同样的事情,Bike 和 BikeI18N 也没有。

如果我在Bike::addTranslation(BikeI18N $translation) 中写入$translation->setAccessori($this);,则会收到错误必须管理传递给选择字段的实体。也许将它们保留在实体管理器中?

如果我不写$translation->setAccessori($this); 表格已保存但关系丢失,或者如果我将数据库设置为not_nullable bike_id 我会收到错误。

对我来说最令人惊讶的是,我在类 Category 的 addTranslation 中有 setCategory($this)...有什么想法吗?

【问题讨论】:

  • 我认为这与您的架构设置无关,请提供您在发生错误时使用的控制器代码。
  • 感谢 m0c 的回答。我没有看到你的活动。我登录通知我发现了问题,我意识到你试图提供帮助。对不起,谢谢!

标签: symfony doctrine-orm


【解决方案1】:

我解决了这个问题。

想法在我的 BikeI18N 表单生成器中。

    class BikeI18NType extends AbstractType
    {
       public function buildForm(FormBuilderInterface $builder, array $options)
         {
            $builder
            ->add('descripcio')
            ->add('locale', 'hidden')
            //->add('bike', 'hidden')
           ;
         }
     }

当我注释掉自行车添加时,问题就解决了。

感谢阅读!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2011-07-21
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多