1

我仍在处理类别嵌套树表单输出和提交的小挑战:Symfony2 实体集合填充选择框

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

我有一个一般形式:

类别表格

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)
    {
        ...
    }
}

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

类别集合类型

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';
    }
}

但我得到的是: 在此处输入图像描述

但我希望在第一组中看到 3 个选择框,在第二组中看到 3 个选择框(数据转换器从以下位置转换数据:

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

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

我的错误在哪里?

我以为我应该在 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'),
                ),
    )
);

但这无济于事......有什么想法吗?

PS>如果你回答,请继续关注这个帖子:)

4

0 回答 0