在嵌入式表单集合中获取实体

Getting entity in embedded collection of forms

在我的编辑表单中,我需要获取嵌入表单中的实体对象。这是我的主要编辑表单:

class OrderCollectionsEditType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('sampleCollections', CollectionType::class, [
                'entry_type' => SampleCollectionType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false
            ])
        ;
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Order::class,
        ]);
    }
}

和嵌入的:

class SampleCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $sampleCollection = $builder->getData();
        $builder
            ->add('methods', EntityType::class, [
                'class' => Method::class,
                'multiple' => true,
            ])
            {...}
        ;
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => SampleCollection::class,
        ]);
    }
}

在控制器中创建的表单:

$form = $this->createForm(OrderCollectionsEditType::class, $order);

问题是 $sampleCollection returns NULL,但表格中的值已正确填写。还有其他方法可以获取实体对象吗?

对象被传递到$options['data]属性中的表单。 而不是 $sampleCollection = $builder->getData(); 通过 $sampleCollection = $options['data];

获取它

不幸的是,上面建议的 $options['data'] 不适用于 CollectionType,没有 'data' 索引。经过更深入的研究,我找到了解决方案,我们可以使用 PRE_SET_DATA 表单事件,然后在监听函数中获取实体对象。

解决方案:

class SampleCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
                $sampleCollection = $event->getData();

                $form = $event->getForm();
                $form->add('methods', EntityType::class, [
                    'class' => Method::class,
                    'multiple' => true,
                ]);
            }
        ;
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => SampleCollection::class,
        ]);
    }
}