title: Call to a member function removeElement() on a non-object
body: <p>I'm creating a form to be able to send an email message to a group of students which are linked to a particular course. By default all students for a given course must be selected, but the sender of the message must be able to deselect students to exclude them from receiving the message. Sending to the whole group is no problem. The problem occurs when removing students from the select.</p>
  
  <p>I'm using Sonata Admin's sonata_type_model with a custom query. On the resulting form, if I don't change the select options and submit the form everything works fine. When I remove an item from the list I get an error after submitting the form: </p>
  
  <p>Error: Call to a member function removeElement() on a non-object in /xxx/xxx/xxx/vendor/sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php line 607</p>
  
  <p>After two days of searching for an answer hopefully someone here can help me in the right direction. Here's some of the code I use: </p>
  
  <p>Admin:</p>
  
  <pre><code>$em = $this-&gt;modelManager-&gt;getEntityManager('Stnu\EduBundle\Entity\DealItem');
      $query = $em-&gt;createQueryBuilder('d')
              -&gt;select('d')
              -&gt;from('StnuEduBundle:DealItem', 'd')
              -&gt;innerJoin('d.deal', 'de')
              -&gt;where('d.course = :course')
              -&gt;andWhere('de.status = :status')
              -&gt;setParameter('course',$course)
              -&gt;setParameter('status','order');
  
      $defaults = $query-&gt;getQuery()-&gt;getResult();
  
  
      $formMapper
              -&gt;with('Certificaten verzenden cursus \''. $title .'\'', array('description' =&gt; 'Begeleidende tekst e-mail'))
                  -&gt;add('dealItems', 'sonata_type_model', array(
                      'required' =&gt; true,
                      'expanded' =&gt; false,
                      'btn_add' =&gt; false,
                      'multiple' =&gt; true,
                      'label' =&gt; 'Verzenden aan',
                      'query' =&gt; $query,
                      'property' =&gt; 'deal.user',
                      'data' =&gt; $defaults,
                      'validation_groups' =&gt; false
                  ))
                  -&gt;add('subject', 'text', array('required' =&gt; true, 'label' =&gt; 'Onderwerp', 'data' =&gt; $subject))
                  -&gt;add('body', 'textarea', array('label' =&gt; 'Bericht', 'required' =&gt; false, 'data' =&gt; $body, 'attr' =&gt; array('class' =&gt; 'tinymce', 'data-theme' =&gt; 'fullpage', 'style' =&gt; 'height: 350px')));
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>/**
   * Create action
   *
   * @return Response
   *
   * @throws AccessDeniedException If access is not granted
   */
  public function createAction()
  {
  
      // the key used to lookup the template
      $templateKey = 'edit';
  
      if (false === $this-&gt;admin-&gt;isGranted('CREATE')) {
          throw new AccessDeniedException();
      }
  
      $object = $this-&gt;admin-&gt;getNewInstance();
  
      $this-&gt;admin-&gt;setSubject($object);
  
  
      /** @var $form \Symfony\Component\Form\Form */
      $form = $this-&gt;admin-&gt;getForm();
      $form-&gt;setData($object);
  
  
      if ($this-&gt;getRestMethod()== 'POST') {
  
          $object-&gt;setDealItems($object-&gt;getDealItems());
  
          $form-&gt;submit($this-&gt;get('request'));
  </code></pre>
  
  <p>The error appears right after this point.</p>
  
  <p>Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Stnu\EduBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * DocsEmail
   * 
   * @ORM\Entity
   */
  class CertificateEmail {
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
  
      /**
       * @ORM\ManyToMany(targetEntity="DealItem")
       * @ORM\JoinTable(name="certificateemails_dealitems",
       *      joinColumns={@ORM\JoinColumn(name="certificateEmail_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="dealItem_id", referencedColumnName="id")}
       *      )
       */
      private $dealItems;
  
      private $subject;
  
      private $body;
  
      private $extraEmailTo;
  
      public function __construct() {
          $this-&gt;dealItems = new ArrayCollection();
      }
  
      /**
       * Add dealItem
       *
       */
      public function addDealItem(\Stnu\EduBundle\Entity\DealItem $dealItem) {
  
          $this-&gt;dealItems-&gt;add($dealItem);
          //$this-&gt;dealItems[] = $dealItem;
          return $this;
      }
  
      /**
       * Remove dealItem
       */
      public function removeDealItem(\Stnu\EduBundle\Entity\DealItem $dealItem) {
  
          foreach ($this-&gt;dealItems as $item) {
              if ($dealItem === $item) {
                  // manager of Stnu\EduBundle\Entity\DealItem
                  $entityManager-&gt;remove($dealItem);
              }
          }
  
      }
  
      /**
       * Get dealItems
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getDealItems() {
  
          return $this-&gt;dealItems;
      }
  
      public function setDealItems($dealItems) {
          $this-&gt;dealItems = new ArrayCollection();
  
          if (count($dealItems) &gt; 0) {
              foreach ($dealItems as $dealItem) {
                  $this-&gt;addDealItem($dealItem);
              }
          }
  
          return $this;
      }
  
  
      /**
       * Set subject
       *
       * @param string $subject
       */
      public function setSubject($subject) {
          $this-&gt;subject = $subject;
  
          return $this;
      }
  
      /**
       * Get subject
       *
       * @return string 
       */
      public function getSubject() {
          return $this-&gt;subject;
      }
  
      /**
       * Set body
       *
       * @param string $body
       */
      public function setBody($body) {
          $this-&gt;body = $body;
  
          return $this;
      }
  
      /**
       * Get body
       *
       * @return string 
       */
      public function getBody() {
          return $this-&gt;body;
      }
  
      /**
       * Set extraEmailTo
       *
       * @param string $extraEmailTo
       */
      public function setExtraEmailTo($extraEmailTo) {
          $this-&gt;extraEmailTo = $extraEmailTo;
  
          return $this;
      }
  
      /**
       * Get extraEmailTo
       *
       * @return string 
       */
      public function getExtraEmailTo() {
          return $this-&gt;extraEmailTo;
      }
  
  }
  </code></pre>
  
  <p>Hopefully someone can help me out!        </p>