在Symfony2中使用自定义字段类型的实体集合呈现表单

时间:2013-09-08 14:04:07

标签: php forms symfony collections dynamic-forms

我仍在使用类别嵌套树形式输出和提交进行小型挑战:Symfony2 entity collection populating selectboxes

我试图首先找到渲染部分(在JS和填充之前)。

我有一般形式:

CategoryForm

class CategoryForm extends AbstractType
{
    private $em;

    public function __construct($em) {
       $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
      $transformer = new CategoryToChoiceTransformer($this->em);

      $builder->add(
               $builder->create('categories', 'collection',
                   array(
                      'type' => new CategoryCollectionType($this->em)
                   )
               )->addModelTransformer($transformer));



        $builder->add('save', 'submit');
    }

    public function getDefaultOptions(array $options)
    {
        $resolver->setDefaults(array(
                'data_class' => 'Acme\DemoBundle\Entity\Item',
        ));

    }

    public function getName()
    {
        return 'category';
    }
}

我有CategoryToChoiceTransformer:

CategoryToChoiceTransformer

class CategoryToChoiceTransformer implements DataTransformerInterface
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @param ObjectManager $om
     */
    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    public function transform($categories)
    {
        $categoriesExpanded = new ArrayCollection();

        if (!empty($categories)) {
            $categoryRepository = $this->om->getRepository('AcmeDemoBundle:Category');

            foreach ($categories as $category) {
                $path = new ArrayCollection($categoryRepository->getPath($category));
                $categoriesExpanded->add($path);
            }
        }

        return $categoriesExpanded;
    }

    public function reverseTransform($categoryList)
    {
        ...
    }
}

我有自定义类型(我在父表单中使用):

CategoryCollectionType

class CategoryCollectionType extends AbstractType
{
    private $em;

    public function __construct($em) {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('name', 'entity', array(
                'class' => 'AcmeDemoBundle:Category',
                'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                        ->orderBy('c.id', 'ASC');
                 },
                'property' => 'name',
                'empty_value' => _('Choose Category')
        ));


    }

    public function getName()
    {
        return 'category_collection_type';
    }
}

但我得到的是: enter image description here

但我希望在第一组中看到3个选择框,在第二组中看到3个选择框(数据转换器转换数据来自:

array(
    [0] = >ObjectCategory#...,
    [1] => ObjectCategory#...
)

array(
    [0] => array( ObjectCategory#..., ObjectCategory#..., ObjectCategory#..., ),
    [1] => array( ObjectCategory#..., ObjectCategory#..., ObjectCategory#..., )
)

我的错误在哪里?

我以为我应该在CategoryCollectionType中执行集合类型:

CategoryCollectionType

...
$builder->add('categories', 'collection', array(
    'type' => 'entity',
    'allow_add' => true,
    'allow_delete' => true,
    'prototype' => false,
    'show_legend' => true,
    'widget_add_btn' => array('label' => _('Add category')),
    'options' => array(
                    'widget_control_group' => false,
                    'label_render' => false,
                    'show_legend' => false,
                    'class' => 'AcmeDemoBundle:Category',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                            ->orderBy('c.id', 'ASC');
                    },
                    'property' => 'name',
                    'empty_value' => _('Choose Category'),
                ),
    )
);

但它没有帮助......任何想法?

P.S>如果您回答,请继续关注此主题:)

0 个答案:

没有答案