从 sf2.8 到 sf3 的集合类型迁移

Collection type migration from sf2.8 to sf3

让我们考虑一下这个例子:我有一个表单类型,我将另一种类型嵌入到 "collection fashion" 中,如下所示

->add('foo', CollectionType::class, [
    'entry_type'     => new FooType(),
    'error_bubbling' => false,
    'allow_add'      => true,
    'allow_delete'   => true,
    'required'       => false,
    'label'          => ' ',
    'entry_options'  => [
        'bar' => $options['bar'],
    ],
    'by_reference' => false,
])

一切正常(我可以轻松添加和删除元素)。

自从我将框架从 2.8 升级到 3.0 后,我阅读了变更日志并注意到了

The option type of the CollectionType has been removed in favor of the entry_type option. The value for the entry_type option must be the fully-qualified class name (FQCN).

所以我已将表格更改为

->add('foo', CollectionType::class, [
    'entry_type'     => FooType::class,
    'error_bubbling' => false,
    'allow_add'      => true,
    'allow_delete'   => true,
    'required'       => false,
    'label'          => ' ',
    'entry_options'  => [
        'bar' => $options['bar'],
    ],
    'by_reference' => false,
])

但是当我将元素添加到集合中时,ModelData 似乎折叠到同一个元素(集合的最后一个)中。

所以,假设我 post id 为 [1,2,3] 的实体,ViewData 是正确的,而 ModelData 有一个包含三个元素的数组集合:三个 foo(s) id 为 3,所以相同的 foo.

有人知道这里发生了什么吗?

编辑

这是FooType有意义的代码

$resolver->setDefaults([
    'data_class' => 'Vendor\Bundle\Entity\Foo',
    'empty_data' => new Foo(),
]);

这似乎是一个 symfony 错误(未经证实;read more)或滥用 entry_type 中的 FQCN 和 empty_data 中的 new 嵌入形式的组合.

当 SF 核心团队在问题中回复我时,我会正确更新我的答案。