Symfony 2表单实体字段类型分组 [英] Symfony 2 Forms entity Field Type grouping

查看:83
本文介绍了Symfony 2表单实体字段类型分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用映射到 Reservation 实体的data_class来呈现Symfony2中的表单,并且此表单具有类 Service 预订服务类之间的关系是多对多的。然后服务具有 ServiceType ,这是另一个类,它从 Service class



我想要做的是在预订表单中将所有服务显示为复选框,并按服务类型进行分组。到目前为止,我可以像这样显示所有服务(代码来自 ReservationType class): $ builder-> add('services','entity',array(
'class'=>'MyBundle:Service',
'multiple'=> ; true,
'expanded'=> true
));

并以默认方式显示表单:

 < form action ={{path('reservations',{'step':2})}}method =post{{form_enctype(form)}}> ; 
{{form_widget(form))}

< input type =submit/>
< / form>

结果如下所示:

 □servicetype1选项
□servicetype1另一个选项
□servicetype2选项
□servicetype2另一个选项

我想达到的是:

  servicetype1:
□选项
□另一个选项
servicetype2:
□选项
□另一个选项

我试图通过使用如下所示的query_builder选项来指定仅服务子集:

  $ builder-> add('services','entity',array(
'class'=>'MyBundle:Service',
'multiple'=> true,
'
'query_builder'=>函数(\My\Bundle\Entity\ServiceRepository $ repository)
{return $ repository-> createQueryBuilder('s' ) - > wher e('s.serviceType =?1') - > setParameter(1,1);}));
$ builder-> add('services','entity',array(
'class'=>'MyBundle:Service',
'multiple'=> true,
'expanded'=> true,
'query_builder'=>函数(\My\Bundle\Entity\ServiceRepository $ repository)
{return $ repository-> createQueryBuilder ('s') - >其中('s.serviceType =?1') - > setParameter(1,2);}));

这是错误的,因为:


  1. 我必须指定 ServiceType id

  2. 添加'services'到构建器两次,将覆盖第一个添加(这是合乎逻辑的,但不能在不更改实体的情况下解决)

处理这种表格的最佳选择是什么?到目前为止,只有2 ServiceType -s,但我想保持它的动态性和可重用性。

解决方案

这可以通过使用 group_by选项

  $ builder-> add('services','entity', array(
'class'=>'MyBundle:Service',
'group_by'=>'serviceType',
'multiple'=> true,
'展开'=> true
));


I'm rendering a form in Symfony2 with data_class mapped to Reservation entity, and this form has a entity field type, of class Service. The relation between Reservation and Service class is many to many. A service then has a ServiceType, which is another class, that is mapped as many to one from the Service class

What I want to do is display all services as checkboxes in the reservation form, grouped by service type. So far, I can display all the services together like this (the code is from ReservationType class):

$builder->add('services','entity', array(
         'class' => 'MyBundle:Service',
         'multiple' => true,
         'expanded'  => true
 ));

And displaying the form the default way:

<form action="{{ path('reservations', {'step': 2}) }}" method="post" {{    form_enctype(form) }}>
   {{ form_widget(form) }}

   <input type="submit" />
</form> 

The result is something like this:

 □ servicetype1 option
 □ servicetype1 another option
 □ servicetype2 option
 □ servicetype2 another option

What I would like to achieve is:

servicetype1:
  □ option
  □ another option
servicetype2:
  □ option
  □ another option

I was trying to specify only subsets of services by using query_builder option like this:

    $builder->add('services','entity', array(
            'class' => 'MyBundle:Service',
            'multiple' => true,
            'expanded' => true,
            'query_builder' => function (\My\Bundle\Entity\ServiceRepository $repository)
                {return $repository->createQueryBuilder('s')->where('s.serviceType = ?1')->setParameter(1, 1);} ));
    $builder->add('services','entity', array(
            'class' => 'MyBundle:Service',
            'multiple' => true,
            'expanded' => true,
            'query_builder' => function (\My\Bundle\Entity\ServiceRepository $repository)
                {return $repository->createQueryBuilder('s')->where('s.serviceType = ?1')->setParameter(1, 2);} ));

This is wrong, because:

  1. I have to specify the ServiceType id
  2. Adding the 'services' to the builder twice, will overwrite the first addition (which is logical, but cannot be solved without changing the entities)

What would be the best option for handling forms like this? There are only 2 ServiceType-s so far, but I would like to keep it dynamic, and reusable.

解决方案

This can be solved by using the group_by option:

$builder->add('services','entity', array(
    'class' => 'MyBundle:Service',
    'group_by' => 'serviceType',
    'multiple' => true,
    'expanded'  => true
));

这篇关于Symfony 2表单实体字段类型分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆