title: Symfony 2 Forms entity Field Type grouping
body: <p>I'm rendering a form in Symfony2 with data_class mapped to <code>Reservation</code> entity, and this form has a entity field type, of class <code>Service</code>. The relation between <code>Reservation</code> and <code>Service</code> class is many to many. A service then has a <code>ServiceType</code>, which is another class, that is mapped as many to one from the <code>Service</code> class</p>
  
  <p>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 <code>ReservationType</code> class):</p>
  
  <pre><code>$builder-&gt;add('services','entity', array(
           'class' =&gt; 'MyBundle:Service',
           'multiple' =&gt; true,
           'expanded'  =&gt; true
   ));
  </code></pre>
  
  <p>And displaying the form the default way:</p>
  
  <pre><code>&lt;form action="{{ path('reservations', {'step': 2}) }}" method="post" {{    form_enctype(form) }}&gt;
     {{ form_widget(form) }}
  
     &lt;input type="submit" /&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>The result is something like this:</p>
  
  <pre><code> □ servicetype1 option
   □ servicetype1 another option
   □ servicetype2 option
   □ servicetype2 another option
  </code></pre>
  
  <p>What I would like to achieve is:</p>
  
  <pre><code>servicetype1:
    □ option
    □ another option
  servicetype2:
    □ option
    □ another option
  </code></pre>
  
  <p>I was trying to specify only subsets of services by using query_builder option like this:</p>
  
  <pre><code>    $builder-&gt;add('services','entity', array(
              'class' =&gt; 'MyBundle:Service',
              'multiple' =&gt; true,
              'expanded' =&gt; true,
              'query_builder' =&gt; function (\My\Bundle\Entity\ServiceRepository $repository)
                  {return $repository-&gt;createQueryBuilder('s')-&gt;where('s.serviceType = ?1')-&gt;setParameter(1, 1);} ));
      $builder-&gt;add('services','entity', array(
              'class' =&gt; 'MyBundle:Service',
              'multiple' =&gt; true,
              'expanded' =&gt; true,
              'query_builder' =&gt; function (\My\Bundle\Entity\ServiceRepository $repository)
                  {return $repository-&gt;createQueryBuilder('s')-&gt;where('s.serviceType = ?1')-&gt;setParameter(1, 2);} ));
  </code></pre>
  
  <p>This is wrong, because:</p>
  
  <ol>
  <li>I have to specify the <code>ServiceType</code> id </li>
  <li>Adding the <code>'services'</code> to the builder twice, will overwrite the first addition (which is logical, but cannot be solved without changing the entities)</li>
  </ol>
  
  <p>What would be the best option for handling forms like this? There are only 2 <code>ServiceType</code>-s so far, but I would like to keep it dynamic, and reusable.</p>