title: php How to remove entity from form collection in Symfony2?
body: <p>In my app I have one entity <code>Content</code> and <code>Episode</code>. Content to Episode is <em>oneToMany</em> relation. Means content may have many episodes.</p>
  
  <p>I used a form collection to add episodes from content form. I exactly followed this documentation: </p>
  
  <p><a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow noreferrer">http://symfony.com/doc/current/cookbook/form/form_collections.html</a></p>
  
  <p>I have been able to add episodes successfully but the problem is I am not able to delete them from the database. The delete button in the frontend works, but when I click update the deleted episode shows up again.</p>
  
  <p>Here is my JavaScript code:</p>
  
  <pre><code>var $collectionHolder;
  
  // setup an "add a tag" link
  var $addTagLink = $('&lt;a href="#" class="add_tag_link"&gt;Add a tag&lt;/a&gt;');
  var $newLinkLi = $('&lt;li&gt;&lt;/li&gt;').append($addTagLink);
  
  jQuery(document).ready(function() {
      // Get the ul that holds the collection of tags
      $collectionHolder = $('ul.collection');
  
      // add a delete link to all of the existing tag form li elements
      $collectionHolder.find('li').each(function() {
          addTagFormDeleteLink($(this));
      });
  
  
      // add the "add a tag" anchor and li to the tags ul
      $collectionHolder.append($newLinkLi);
  
      // count the current form inputs we have (e.g. 2), use that as the new
      // index when inserting a new item (e.g. 2)
      $collectionHolder.data('index', $collectionHolder.find(':input').length);
  
      $addTagLink.on('click', function(e) {
          // prevent the link from creating a "#" on the URL
          e.preventDefault();
  
          // add a new tag form (see next code block)
          addTagForm($collectionHolder, $newLinkLi);
      });
  
  });
  
  function addTagForm($collectionHolder, $newLinkLi) {
      // Get the data-prototype explained earlier
      var prototype = $collectionHolder.data('prototype');
  
      // get the new index
      var index = $collectionHolder.data('index');
  
      // Replace '__name__' in the prototype's HTML to
      // instead be a number based on how many items we have
      var newForm = prototype.replace(/__name__/g, index);
  
      // increase the index with one for the next item
      $collectionHolder.data('index', index + 1);
  
      // Display the form in the page in an li, before the "Add a tag" link li
      var $newFormLi = $('&lt;li&gt;&lt;/li&gt;').append(newForm);
      $newLinkLi.before($newFormLi);
  
      // add a delete link to the new form
      addTagFormDeleteLink($newFormLi);
  }
  
  function addTagFormDeleteLink($tagFormLi) {
      var $removeFormA = $('&lt;a href="#"&gt;delete this tag&lt;/a&gt;');
      $tagFormLi.append($removeFormA);
  
      $removeFormA.on('click', function(e) {
          // prevent the link from creating a "#" on the URL
          e.preventDefault();
  
          // remove the li for the tag form
          $tagFormLi.remove();
      });
  }
  </code></pre>
  
  <p>Update action:</p>
  
  <pre><code>$content = $em-&gt;find('MyAppBundle:Content', $id);
  
  if (!$content) {
      throw $this-&gt;createNotFoundException('Unable to find Content entity.');
  }
  
  $originalEpisodes = $content-&gt;getEpisodes()-&gt;toArray();
  
  $deleteForm = $this-&gt;createDeleteForm($id);
  $editForm   = $this-&gt;createEditForm($content, $this-&gt;generateUrl('content_update', array('id' =&gt; $content-&gt;getId())), 'PUT', 'Update');
  
  
  $editForm-&gt;handleRequest($request);
  
  
  if ($editForm-&gt;isValid()) {
  
      foreach ($originalEpisodes as $episode) {
  
          if (false === $content-&gt;getEpisodes()-&gt;contains($episode)) {
              // remove the Task from the Tag
              $content-&gt;getEpisodes()-&gt;removeElement($episode);
  
              // if it was a many-to-one relationship, remove the relationship like this
              // $tag-&gt;setTask(null);
  
              $em-&gt;persist($episode);
  
              // if you wanted to delete the Tag entirely, you can also do that
              // $em-&gt;remove($tag);
          }
      }
  
      $em-&gt;persist($content);
      $em-&gt;flush();
  
      return $this-&gt;redirect($this-&gt;generateUrl('content_edit', array('id' =&gt; $id)));
  }
  </code></pre>
  
  <p>Entity:</p>
  
  <pre><code>/**
   * @var \Doctrine\Common\Collections\Collection
   * @ORM\OneToMany(targetEntity="Episode", mappedBy="content", cascade={"remove,persist"}, orphanRemoval=true)
   */
  private $episodes;
  
  public function removeEpisode(Episode $episode)
  {
      //$this-&gt;episodes-&gt;removeElement($episode);
  
      //return $this;
  
      $this-&gt;episodes-&gt;remove($episode);
      $episode-&gt;setContent(null);
  }
  </code></pre>
  
  <p>in my controller when i debug <code>$content-&gt;getEpisodes()-&gt;contains($episode)</code> i see all returns are true not false, so I'm thinking is this the only reason my delete function not working??</p>
  
  <p>I even tried this also :</p>
  
  <p><a href="https://stackoverflow.com/questions/8923919/symfony2-form-collection-how-to-remove-entity-from-a-collection">Symfony2 form collection: How to remove entity from a collection?</a></p>
  
  <p>but not working. please help</p>
  
  <p>Episode Entity as per request:</p>
  
  <pre><code>&lt;?php
  
  namespace Bbd\BongoAppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
  * Episode
  */
    class Episode
   {
  /**
   * @var integer
   */
  private $id;
  
  /**
   * @var string
   */
  private $unique_id;
  
  /**
   * @var Content
   */
  private $content;
  
  /**
   * @var string
   */
  private $number;
  
  /**
   * @var string
   */
  private $duration;
  
  /**
   * @var string
   */
  private $kaltura_id;
  
  /**
   * @var integer
   */
  private $views;
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set content
   *
   * @param Content $content
   *
   * @return $this
   */
  public function setContent(Content $content)
  {
      $this-&gt;content = $content;
      return $this;
  }
  
  /**
   * Get content
   *
   * @return Content
   */
  public function getContent()
  {
      return $this-&gt;content;
  }
  
  /**
   * Set number
   *
   * @param string $number
   * @return $this
   */
  public function setNumber($number)
  {
      $this-&gt;number = $number;
      return $this;
  }
  
  /**
   * Get number
   *
   * @return string
   */
  public function getNumber()
  {
      return $this-&gt;number;
  }
  
  /**
   * Set duration
   *
   * @param string $duration
   * @return $this
   */
  public function setDuration($duration)
  {
      $this-&gt;duration = $duration;
      return $this;
  }
  
  /**
   * Get duration
   *
   * @return string
   */
  public function getDuration()
  {
      return $this-&gt;duration;
  }
  
  /**
   * Set kalturaId
   *
   * @param string $kalturaId
   * @return $this
   */
  public function setKalturaId($kalturaId)
  {
      $this-&gt;kaltura_id = $kalturaId;
      return $this;
  }
  
  /**
   * Get kalturaId
   *
   * @return string
   */
  public function getKalturaId()
  {
      return $this-&gt;kaltura_id;
  }
  
  /**
   * Set views
   *
   * @param integer $views
   * @return $this
   */
  public function setViews($views)
  {
      $this-&gt;views = $views;
      return $this;
  }
  
  /**
   * Get views
   *
   * @return integer
   */
  public function getViews()
  {
      return $this-&gt;views;
  }
  
  /**
   * Get sequence number
   *
   * @return integer|null
   */
  public function getSequenceNumber()
  {
  
      return  $this-&gt;getSequenceCount();
  
  }
  
  /**
   * Set uniqueId
   * @param string $uniqueId
   * @return $this
   */
  public function setUniqueId($uniqueId)
  {
      $this-&gt;unique_id = $uniqueId;
      return $this;
  }
  
  /**
   * Get uniqueId
   *
   * @param boolean $regenerate
   *
   * @return string
   */
  public function getUniqueId()
  {
  
      $content   = $this-&gt;getContent();
  
      $uniqueId  = '';
      $uniqueId .= $content-&gt;getContentOwner()-&gt;getSlug();
      $uniqueId .= $content-&gt;getSequence()[0];
      $uniqueId .= $this-&gt;getSequenceNumber();
      $uniqueId .= $content-&gt;getCategory()-&gt;getSlug();
      $uniqueId .= $content-&gt;getCategorySequence();
  
      $this-&gt;unique_id = strtoupper($uniqueId);
  
      return $this-&gt;unique_id;
  }
  /**
   * @var integer
   */
  private $sequence_count;
  
  
  /**
   * Set sequence_count
   *
   * @param integer $sequenceCount
   * @return Episode
   */
  public function setSequenceCount($sequenceCount)
  {
      $this-&gt;sequence_count = str_pad($sequenceCount, 3, '0', STR_PAD_LEFT);
  
      return $this;
  }
  
  /**
   * Get sequence_count
   *
   * @return integer
   */
  public function getSequenceCount()
  {
      return $this-&gt;sequence_count;
  }
  /**
   * @var string
   */
  private $status;
  
  
  /**
   * Set status
   *
   * @param string $status
   * @return Episode
   */
  public function setStatus($status)
  {
      $this-&gt;status = $status;
  
      return $this;
  }
  
  /**
   * Get status
   *
   * @return string
   */
  public function getStatus()
  {
      return $this-&gt;status;
  }
  /**
   * @var string
   */
  private $bangla_number;
  
  
  /**
   * Set bangla_number
   *
   * @param string $banglaNumber
   * @return Episode
   */
  public function setBanglaNumber($banglaNumber)
  {
      $this-&gt;bangla_number = $banglaNumber;
  
      return $this;
  }
  
  /**
   * Get bangla_number
   *
   * @return string 
   */
  public function getBanglaNumber()
  {
      return $this-&gt;bangla_number;
  }
  }
  </code></pre>