title: Symfony: update many-to-many
body: <p>I have <code>Post</code> and <code>Tag</code> entities with many-to-many relationship. In <code>Post</code> create and edit form there is a textbox where I can enter tags separated by comma relevant to that post. For example, when I enter tag1, tag2, tag3 for post with title 'Post1', the form will create post and tag entities and add these tags to tags list of that post.I use data transformer to create tag entities.</p>
  
  <pre><code>class Post{
      /**
       * @ORM\ManyToMany(targetEntity="Tag", mappedBy="posts",cascade={"all"})
       */ 
      protected $tags;
  
      public function __construct() {
              $this-&gt;tags = new ArrayCollection();
          }
  
      /**
       * @return ArrayCollection
       */
      public function getTags()
      {
          return $this-&gt;tags;
      }
  
      /**
       * @param Tag $tag
       */
      public function addTag(Tag $tag)
      {
          $tag-&gt;addPost($this);
  
          $this-&gt;tags-&gt;add($tag);
      }
  
      /**
       * @param Tag $tag
       */
      public function removeTag(Tag $tag)
      {
          $this-&gt;tags-&gt;removeElement($tag);
      }
  }
  </code></pre>
  
  <p>PostType</p>
  
  <pre><code> public function buildForm(FormBuilderInterface $builder, array $options)
  {
  
      $builder
          -&gt;add('title', TextType::class, array('label' =&gt; 'Title'))
          -&gt;add('tags', TextType::class, array('label' =&gt; 'Tags'))
      ;
  
      $builder-&gt;get('tags')
          -&gt;addModelTransformer(new TagViewTransformer($this-&gt;manager));
  }
  </code></pre>
  
  <p>TagViewTransformer</p>
  
  <pre><code>class TagViewTransformer implements DataTransformerInterface
  {
  
  public function transform($value)
  {
      /...
  }
  
  public function reverseTransform($value)
  {
      $tags = array();
      if ( $value )
      {
          if( strpos($value, ',') !== false )
          {
              $list = array_unique(explode(",", $value));
          }
          else
          {
              $list = array(trim($value));
          }
  
          foreach ( $list AS $tagName )
          {
              $tag = $this-&gt;em
                  -&gt;getRepository('CoreBundle:Tag')
                  -&gt;findOneBy(array('name' =&gt; trim($tagName)));
  
              if( !$tag )
              {
                  $tag = new Tag();
                  $tag-&gt;setName(trim($tagName));
                  $this-&gt;em-&gt;persist($tag);
              }
  
              $tags[] = $tag;
          }
       }
      return $tags;
      }
  }
  </code></pre>
  
  <p>This works fine when I try to create Post, all tags are transformed to entities and are added to Post's tags list. but when I try to edit,  I start having problems</p>
  
  <pre><code> public function editAction(Request $request, Post $post)
  {
      $deleteForm = $this-&gt;createDeleteForm($post);
      $editForm = $this-&gt;createForm(PostType::class, $post);
      $editForm-&gt;handleRequest($request);
  
      $originalTags = $post-&gt;getTags();
  
      if ($editForm-&gt;isSubmitted() &amp;&amp; $editForm-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $newTags = $editForm-&gt;get('tags')-&gt;getData();
  
          foreach ($originalTags as $currentTag) {
              if (!in_array($currentTag, $newTags)) {
                  $post-&gt;removeTag($currentTag);
              }
          }
  
          $em-&gt;persist($post);
          $em-&gt;flush();
  
          return $this-&gt;redirectToRoute('post_show', array('id' =&gt; $post-&gt;getId()));
      }
  
      return $this-&gt;render('AppBundle:Post:edit.html.twig', array(
          'entity' =&gt; $post,
          'form' =&gt; $editForm-&gt;createView(),
          'delete_form' =&gt; $deleteForm-&gt;createView(),
      ));
  }
  </code></pre>
  
  <p>Let's say Post has tags: <code>tag1, tag2, tag3</code>, but I want to remove <code>tag3</code> and add <code>tag4</code>. So I will change tags textbox to <code>tag1, tag2, tag4</code>. However when I submit form, I get <code>tag1, tag2, tag3, tag4</code>. So tag3 is not removed from Post's tag list. </p>
  
  <p>What is wrong with the editAction code?</p>