title: How to properly display multiple CollectionType fields using symfony3.x
body: <p>Hello I'm trying to model an Entity that uses several different collections. I tried out the <a href="https://github.com/ninsuo/symfony-collection" rel="nofollow noreferrer">https://github.com/ninsuo/symfony-collection</a> project and it offers a wide range of useful options.  The closest example I've seen is the one with Collection of Collections where one Entity has several collections of the SAME child EntityType.  I'm trying to achieve the same behaviour with several collections of DIFFERENT child EntityTypes.</p>
  <p>The problem I face with my Entity is that when I put only one collection in it, the code works ok, but when I add a second collection of a different child Entity and send my form, my controller code ends up deleting the other collection's elements. I narrowed it down to the view, hence why I'm asking about that specific project.</p>
  <p>I'm currently using Symfony 3.x, and have been able to follow the listed examples up to the point of working well with one collection only, I'm able to ADD, DELETE and UPDATE.</p>
  <p>My controller code:</p>
  <pre class="lang-php prettyprint-override"><code>namespace SigavFileBundle\Form;
  
  use Ivory\CKEditorBundle\Form\Type\CKEditorType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use SigavFileBundle\Entity\BookingAccommodation;
  use SigavFileBundle\Entity\BookingIncludes;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\DateType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  use Doctrine\ORM\EntityRepository;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  
  class BookingType extends AbstractType
  {
      /**
      * {@inheritdoc}
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('flights', CollectionType::class, 
                  array(
                      'entry_type' =&gt; FlightDataType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__flights-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;flights-collection&quot;,
                      ],
                  )
              )
              -&gt;add('accommodations', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingAccommodationType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__accomm-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;accomm-collection&quot;,
                      ],
                  )
              )
              -&gt;add('cars', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingCarType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__cars-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;cars-collection&quot;,
                      ],
                  )
              )
              -&gt;add('transfers', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingTransferType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__transfers-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;transfers-collection&quot;,
                      ],
                  )
              )
              -&gt;add('excursions', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingExcursionType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__exc-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;exc-collection&quot;,
                      ],
                  )
              )
              -&gt;add('includes', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingIncludesType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__inc-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;inc-collection&quot;,
                      ],
                  )
              )
              -&gt;add('customActivities', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingCustomActivityType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__act-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;act-collection&quot;,
                      ],
                  )
              )
              -&gt;add('guides', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingGuideType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__guides-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;guides-collection&quot;,
                      ],
                  )
              )
              -&gt;add('commentaries', CollectionType::class, 
                  array(
                      'entry_type' =&gt; BookingCommentariesType::class,
                      'allow_add'    =&gt; true,
                      'allow_delete' =&gt; true,
                      'prototype'    =&gt; true,
                      'required'     =&gt; false,
                      'by_reference' =&gt; true,
                      'delete_empty' =&gt; true,
                      'prototype_name' =&gt; '__comm-collection__',
                      'attr'         =&gt; [
                          'class' =&gt; &quot;comm-collection&quot;,
                      ],
                  )
              )
          ;
      }
  
      /**
      * {@inheritdoc}
      */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'SigavFileBundle\Entity\Booking'
          ));
      }
  
      /**
      * {@inheritdoc}
      */
      public function getBlockPrefix()
      {
          return 'sigavfilebundle_booking';
      }
  }
  </code></pre>
  <p>As you can see, multiple collections of different types.  Up next, this is the code for two of those FormTypes only, <strong>BookingAccommodationType</strong> and <strong>BookingCarType</strong>:</p>
  <p>BookingAccommodationType:</p>
  <pre class="lang-php prettyprint-override"><code>namespace SigavFileBundle\Form;
  
  use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  use SigavGeneralBundle\Controller\MealPlanController;
  use SigavGeneralBundle\Entity\Hotel;
  use SigavGeneralBundle\Entity\RoomType;
  use SigavGeneralBundle\SigavGeneralBundle;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\DateType;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Doctrine\ORM\EntityRepository;
  
  class BookingAccommodationType extends AbstractType
  {
      /**
      * {@inheritdoc}
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          ...
          // Attributes go here
          ...
      }
      
      /**
      * {@inheritdoc}
      */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'SigavFileBundle\Entity\BookingAccommodation'
          ));
      }
  
      /**
      * {@inheritdoc}
      */
      public function getBlockPrefix()
      {
          return 'sigavfilebundle_bookingaccommodation';
      }
  }
  </code></pre>
  <p>BookingCarType:</p>
  <pre class="lang-php prettyprint-override"><code>namespace SigavFileBundle\Form;
  
  use Doctrine\ORM\EntityRepository;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\DateType;
  use Symfony\Component\Form\Extension\Core\Type\TimeType;
  
  class BookingCarType extends AbstractType
  {
      /**
      * {@inheritdoc}
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          ...
          // Attributes go here
          ...
      }
      
      /**
      * {@inheritdoc}
      */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'SigavFileBundle\Entity\BookingCar'
          ));
      }
  
      /**
      * {@inheritdoc}
      */
      public function getBlockPrefix()
      {
          return 'sigavfilebundle_bookingcar';
      }
  }
  </code></pre>
  <p>All view related code follows.</p>
  <p>main_view.html.twig:</p>
  <pre class="lang-twig prettyprint-override"><code>&lt;!-- A lot of HTML code before //--&gt;
  {%
      form_theme form.accommodations
      'jquery.collection.html.twig'
      'booking/bookingAccommodations.html.twig'
  %}
  
  {{ form( form.accommodations) }}
  
  &lt;!-- A lot of HTML code between //--&gt;
  
  {%
  form_theme form.cars
  'jquery.collection.html.twig'
  'booking/bookingCars.html.twig'
  %}
  {{ form( form.cars) }}
  
  &lt;!-- A lot of HTML code after //--&gt;
  
  &lt;script&gt;
      function initCollectionHolders( $id, $form_id )
      {
          $($id).collection({
              name_prefix:  $form_id,
              add_at_the_end: true,
              allow_add: 1,
              allow_duplicate: 1,
              allow_remove: 1,
              duplicate: '&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;pe-7s-copy&quot;&gt;&lt;/span&gt;&lt;/a&gt;',
              add: '&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;pe-7s-plus&quot;&gt;&lt;/span&gt;&lt;/a&gt;',
              remove: '&lt;a href=&quot;#&quot;&gt;&lt;span class=&quot;pe-7s-close-circle&quot;&gt;&lt;/span&gt;&lt;/a&gt;'
          });
      }
  
      initCollectionHolders('.accomm-collection', '{{ form.accommodations.vars.full_name }}');
      initCollectionHolders('.cars-collection', '{{ form.cars.vars.full_name }}');
  &lt;script&gt;
  </code></pre>
  <p>bookingCars.html.twig:</p>
  <pre class="lang-twig prettyprint-override"><code>{% block sigavfilebundle_bookingcar_label %}{% endblock %}
  
  {% block sigavfilebundle_bookingcar_widget %}
  
      {# HERE GOES THE ENTIRE FORM LAYOUT #} 
  
  {% endblock %} 
  </code></pre>
  <p>bookingAccommodations.html.twig:</p>
  <pre class="lang-twig prettyprint-override"><code>{% block sigavfilebundle_bookingaccommodation_label %}{% endblock %}
  
  {% block sigavfilebundle_bookingaccommodation_widget %}
  
  {# HERE GOES THE ENTIRE FORM LAYOUT #}
  
  {% endblock %}
  </code></pre>
  <p>jquery.collection.html.twig:</p>
  <pre class="lang-twig prettyprint-override"><code>{% block collection_widget %}
      {% spaceless %}
          {% if prototype is defined %}
              {% set attr = attr|merge({'data-prototype': form_row(prototype)}) %}
              {% set attr = attr|merge({'data-prototype-name': prototype.vars.name}) %}
          {% endif %}
          {% set attr = attr|merge({'data-allow-add': allow_add ? 1 : 0}) %}
          {% set attr = attr|merge({'data-allow-remove': allow_delete ? 1 : 0 }) %}
          {% set attr = attr|merge({'data-name-prefix': full_name}) %}
          {{ block('form_widget') }}
      {% endspaceless %}
  {% endblock collection_widget %}
  </code></pre>
  <p>I wanted to know first if the <strong>symfony-collection</strong> library can be used in an environment with one Entity and multiple different child type collections</p>
  <p>Thanks in advance ...</p>