Symfony2从集合中创建表单

时间:2015-02-18 14:12:44

标签: forms symfony collections types

我需要创建一个表单来编辑实体列表。这与我们有一个实体作为嵌套实体的经典用例(以及我发现的所有示例)不同。

在我的情况下,我想使用:

$form = $this->get('form.factory')->create(
        new MyType(),
        mycollectionofobjects
    );

因此表单需要处理一组对象(在我的例子中,是一个推进集合)。

在经典案例中,我们可以使用:

$formBuilder->add(
            'getTheCollection',
            "collection", etc...

因此,从主要对象开始,使用getTheCollection的数据构建子表单,但是没有主实体,它不起作用。

2 个答案:

答案 0 :(得分:0)

我在这种情况下使用的解决方案如下

  • 创建一个不会持久保存到DB的实体,当然只代表一个容器。像

    这样的东西
    use Doctrine\Common\Collections\ArrayCollection;
    class YourTypeCollector
    {
        protected $your_type_colletion;
    
        public function __construct() 
        {
            $this->your_type_colletion = new ArrayCollection();
        }
    
        public function setYourTypeCollection(ArrayCollection $yourTypeCollection)
        {
            $this->your_type_collection = $yourTypeCollection
        }
    
        public function getYourTypeCollection()
        {
            return $this->your_type_collection;
        }
    }
    
  • 创建YourTypeCollectorForm

    class YourTypeCollectorFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('your_type_collection', 'collection', array(
                'type' => new YourTypeType()
            );
        }
    
       public function getName()
       {
           return 'your_type_collector_form';
       }
    
       public function setDefaultOptions(OptionsResolverInterface $resolver)
       {
           $resolver->setDefaults(array(
               'data_class' => 'Path\To\Your\Type\Collector',
                'cascade_validation' => true, //!Important
           ));
        }
    }
    
  • 将其实例化为控制器并将其用作" normal"其他具有集合的表单

    $your_type_colletor = new YourTypeCollector();
    $your_type_collector->setYourTypeCollection(//an array collection here);
    $form = $this->createForm(new YourTypeCollectorType());
    

我不确定这是解决此问题的最佳方法,但我确信这项工作正如我目前在旧项目中使用的那样

答案 1 :(得分:0)

另一种可能性(相信可以适应):

鉴于存在一个名为Collection的实体,其中包含一组对象,请创建一个表单视图,以便编辑所有(或选定的)对象。

1)为单个对象创建表单类型:

class CollectionType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('object')
            ;
    }

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

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'YourBundle\Entity\Collection',
        ));
    }

2)为一组对象

创建表单类型
class ObjectsType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('objects', 'collection', ['type' => new CollectionType(),
                    ])
                ))
            ;
    }

3)在控制器中实例化表单

public function editObjectsAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $objects = $em->getRepository("YourBundle:Collection")->findAll();
    $form = $this->createForm(new ObjectsType(), array('objects' => $objects));
    ...
}

[希望我正确地概括了我对这个解决方案的使用!]

对于“真实世界”应用,请参阅this SO answer

相关问题