Symfony 2系列收藏

时间:2015-07-21 11:07:41

标签: forms symfony collections

我在symfony 2.6表单中收集了集合。它工作得很好,但是如果在内部集合中我在提交后创建了几个嵌套集合,只保存了最后一个嵌套集合。

class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $sizeGroup = $options['data']->getSizesGroups()->getId();

    $builder
        ->add(
            'colors',
            'collection',
            [
                'label' => 'Colors',
                'attr' => [
                    'class' => 'form-control'
                ],
                'type'         => new ProductToColorsType(),
                'allow_add'    => true,
                'allow_delete' => true,
                'prototype'    => true,
                'by_reference' => false,
                'options'      => [
                    'sizeGroup' => $sizeGroup
                ]
                //'delete_empty' => true,
            ]
        )
    ;
}

// more functions

}

ProductToColorsType :(仅发布buildForm()函数)

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if (empty($options['sizeGroup']))
    {
        throw new \Exception('option "sizeGroup" is empty. Part 2');
    }

    $groupId = $options['sizeGroup'];

    $builder
        ->add(
            'images',
            'collection',
            [
                'label' => 'Pictures',
                'attr' => [
                    'class' => 'form-control'
                ],
                'type'         => new ProductMediaType(),
                'allow_add'    => true,
                'allow_delete' => true,
                'prototype'    => true,
                'by_reference' => false,
                //'delete_empty' => true,
            ]
        )
    ;
}

ProductMediaType(仅发布buildForm()函数)

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'orderNum',
            'integer',
            [
                'label' => 'Order',
                'required' => false,
                'attr' => [
                    'class' => 'form-control'
                ]
            ])
        ->add(
            'file',
            'file',
            [
                'data_class' => null,
                'label' => 'Image',
                'required' => false,
            ])
        ;
}

也许有人知道如何修复它?

感谢。

0 个答案:

没有答案