これを再現する方法:
1.あるタイプのコレクションを作成します
<?php
$builder->add('foo', 'collection', array(
'type' => new BarType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
'prototype_name' => 'this_is_prototype',
'options' => array(
'data_class' => 'Acme\FooBundle\Entity\Bar'
),
));
2.作成するBarType
<?php
class BarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
var_dump(array(
$builder->getForm()->getName() => $builder->hasParent()
));
$builder->add('bar', 'text');
// this actually is not relevant, just adding anything
// so that bar form is not empty
}
}
var_dump()の結果
コレクションに3つのBarオブジェクトが含まれている場合、結果は次のようになります。
array (size=1)
'this_is_prototype' => boolean true
array (size=1)
0 => boolean false
array (size=1)
1 => boolean false
array (size=1)
2 => boolean false
結論
buildForm
$ builder-> getParent()では、プロトタイプの親ビルダーのみが返されます。
問題/質問
いくつかのパラメータを取得するには、親フォームにアクセスする必要があります。既存のコレクション要素の親が削除されるのはなぜですか?回避策はありますか?