title: Best way to handle large Form Collection in Symfony2
body: <p><strong>Edited my post to be similar to Symfony Cookbook and added some code.</strong></p>
  
  <p><a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow">http://symfony.com/doc/current/cookbook/form/form_collections.html</a></p>
  
  <p>Note that the Entity/Form code posted in the part is the same as the one in the doc linked above.</p>
  
  <p>I have a "Task" entity, which is linked to a "Tag" entity.To keep it simple, "Task" has a single field "description", and a "tag" has a single field "name".A "Tag" is linked to one "Task", and a "Task" is linked to many "Tags".</p>
  
  <p><strong>Entity:</strong></p>
  
  <pre><code>class Task
  {
      protected $description;
      protected $tags;
  
      public function __construct()
      { $this-&gt;tags = new ArrayCollection(); }
  
      public function getDescription()
      { return $this-&gt;description;}
  
      public function setDescription($description)
      { $this-&gt;description = $description; }
  
      public function getTags()
      { return $this-&gt;tags; }
  
      public function setTags(ArrayCollection $tags)
      { $this-&gt;tags = $tags; }
  }
  
  class Tag
  {
      public $name;
  }
  </code></pre>
  
  <p>At the moment, I use a Collection of "Tags" in the "Task" form to edit all them at once, as described in Symfony CookBook:</p>
  
  <p><strong>Form:</strong></p>
  
  <pre><code>class TagType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('name');
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\TaskBundle\Entity\Tag',
          ));
      }
  
      public function getName()
      {
          return 'tag';
      }
  }
  
  
  class TaskType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('description');
  
          $builder-&gt;add('tags', 'collection', array('type' =&gt; new TagType()));
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\TaskBundle\Entity\Task',
          ));
      }
  
      public function getName()
      {
          return 'task';
      }
  }
  </code></pre>
  
  <p>But a problem appears when I create a collection of "Tags" larger than a thousand elements. At this time, the form take seconds and seconds to load, and sometimes it crashes due to memory.</p>
  
  <pre><code>$task = new Task();
  
  $tag1 = new Tag();
  $tag1-&gt;name = 'tag1';
  $task-&gt;getTags()-&gt;add($tag1);
  $tag2 = new Tag();
  $tag2-&gt;name = 'tag2';
  $task-&gt;getTags()-&gt;add($tag2);
  //Create a couple thousand more item here ...
  
  //The script crashes here, when the form is being created
  $form = $this-&gt;createForm(new TaskType(), $task);
  </code></pre>
  
  <p>The error does not come from Doctrine, which handles very well the whole thing, but from Symfony Form.</p>
  
  <p>Is it a good idea to handle my form using Symfony2 built-in form system (with Collections) in this case, or should I handle it like in the old days, with raw html inputs and server-side validation/saves?</p>