title: Doctrine does not update a simple array type field
body: <h2>Short Story (Edit)</h2>
  
  <p>It is possible to store an array instead of a mapped association. In Symfony2, this is fairly easy using the <a href="http://symfony.com/doc/current/reference/forms/types/collection.html" rel="nofollow noreferrer">collection Field Type</a>. For example, using this technique, you could store an array of text fields that populate an array events field. However, to update an array, there is a trick, and this trick is beautifully explained by @Vadim Ashikhman in the accepted answer.</p>
  
  <h2>Long Story</h2>
  
  <p>Sometimes it is useful and more efficient to store an array instead of a mapped association. However, once created, it remains complicated to update this Array if the size of that array does not change?</p>
  
  <p>Many people have a <a href="https://stackoverflow.com/questions/11084209/how-to-force-doctrine-to-update-array-type-fields">similar issue</a> but nobody found a proper solution to this problem.</p>
  
  <p><img src="https://i.stack.imgur.com/zuJ7D.png" alt="Store an array"></p>
  
  <p>A team can organise many events. These events are simply stored within an array using Doctrine instead of using a OneToMany association. Therefore, the entity Event is not mapped with Doctrine.</p>
  
  <h2>Entity Event (not mapped with Doctrine)</h2>
  
  <pre><code>&lt;?php
  
  namespace Acme\TestBundle\Entity;
  
  ...
  
  class Event
  {
  
      /**
       * @Assert\NotBlank
       */
      private $name;
  
      public function setName($name)
      {
          $this-&gt;name = $name;
      }
  
      public function getName()
      {
          return $this-&gt;name;
      }
  
  
  }
  </code></pre>
  
  <h2>Entity Team</h2>
  
  <pre><code>&lt;?php
  
  namespace Acme\TestBundle\Entity;
  
  ...
  
  /**
   * @ORM\Entity()
   * @ORM\HasLifecycleCallbacks
   * @ORM\Table(name="teams")  
   */
  class Team 
  {
  
  /**
       * @ORM\Column(type="array")
       * @var array
       */
      protected $events;
  
  
      public function addEvent($event)
      {
          if (!in_array($event, $this-&gt;events, true)) {
              $this-&gt;events[] = $event;
          }
  
          return $this;
      }
  
      public function removeEvent($event)
      {
          if (false !== $key = array_search($event, $this-&gt;events, true)) {
              unset($this-&gt;events[$key]);
              $this-&gt;events = array_values($this-&gt;events);
          }
  
          return $this;
      }
  
      public function getEvents()
      {
          return $this-&gt;events;
      }
  
      public function setEvents(array $events)
      {
          $this-&gt;events = array();
  
          foreach ($events as $event) {
              $this-&gt;addEvent($event);
          }
  
          return $this;
      }
  </code></pre>
  
  <h2> Event Form</h2>
  
  <pre><code>&lt;?php
  namespace Acme\TestBundle\Form\Type;
  
  ...
  
  class EventType extends AbstractType
  {
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          parent::buildForm($builder, $options);
  
          $builder-&gt;add('name', 'text');
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\TestBundle\Entity\Event',
              'cascade_validation' =&gt; true,
          ));
      }
  
      ...
  }
  </code></pre>
  
  <h2> Team Form</h2>
  
  <pre><code>&lt;?php
  
  namespace Acme\TestBundle\Form\Type;
  
  ...
  
  class TeamType extends AbstractType
  {
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          parent::buildForm($builder, $options);
  
          $builder-&gt;add('events','collection', array(
              'type' =&gt; new EventType(),
              'allow_add'   =&gt; true,
              'allow_delete' =&gt; true,
              'prototype' =&gt; true,
              'by_reference' =&gt; false,
              'options' =&gt; array('data_class' =&gt; 'Acme\TestBundle\Entity\Event'),
              )
          );
  
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\TestBundle\Entity\Team',
          ));
      }
      ...
  
  }
  </code></pre>
  
  <h2>The Controller</h2>
  
  <pre><code>/**
   * Update a team
   *
   * @Route("update/{team_id}", name="updateTeamFromId")
   * @Template("AcmeTestBundle:Team:teamUpdate.html.twig")
   */
  public function updateTeamAction($team_id, Request $request)
  {
  
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      $repository= $em-&gt;getRepository('AcmeTestBundle:Team');
  
      $team_to_update = $repository-&gt;find($team_id);
  
      $form = $this-&gt;createForm(new teamType(), $team_to_update);
  
      if ($request-&gt;getMethod() == 'POST')
      {
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()){
  
              $em-&gt;persist($team_to_update);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('homepage'))  ;
          }
      }
  
      return array(
      'form' =&gt; $form-&gt;createView(),
      'team_id' =&gt; $team_id,
      );
  
  }
  </code></pre>