title: Symfony Type error: Argument passed must be an instance of entity or null, ArrayCollection given
body: <p>I get the following error:</p>
  
  <pre><code>Type error: Argument 2 passed to DocumentBundle\Form\SelectionType::DocumentBundle\Form\{closure}() must be an instance of ReferentialBundle\Entity\Channel1 or null, instance of Doctrine\Common\Collections\ArrayCollection given, called in /srv/http/sp/src/DocumentBundle/Form/SelectionType.php on line 133
  </code></pre>
  
  <p>Unfortunately I don't see my mistake in my FormType.</p>
  
  <p>It looks like that:</p>
  
  <pre><code>    class SelectionType extends AbstractType {
  
        protected $em;
  
        public function __construct(\Doctrine\ORM\EntityManager $em)
        {
            $this-&gt;em = $em;
        }
  
  
        public function buildForm(FormBuilderInterface $builder, array $options)
          {
  
  
            $builder
            -&gt;add('channel1s', EntityType::class, array(
  
              'class' =&gt; 'ReferentialBundle:Channel1',
              'property' =&gt; 'name',
              'label' =&gt; 'label.channel1s',
              'empty_value' =&gt; 'label.select_channel1s',
              'mapped' =&gt; false,
              'expanded' =&gt; false,
              'translation_domain' =&gt; 'UploadProfile',
              'multiple' =&gt; true,
              'required' =&gt; false,
            ));
  
            $formModifier = function (FormInterface $form, Channel1 $channel1s = null) {
              $channel3s = null === $channel1s ? array() : $channel1s-&gt;getChannel3s();
  
              $form
              -&gt;add('channel3s', EntityType::class, array(
                'class' =&gt; 'ReferentialBundle:Channel3',
                'choices' =&gt; $channel3s,
              ));
            };
            $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function (FormEvent $event) use ($formModifier) {
                $data = $event-&gt;getData();
                $formModifier($event-&gt;getForm(), $data-&gt;getChannel1s()[0]);
              }
            );
            $builder-&gt;get('channel1s')-&gt;addEventListener(
              FormEvents::POST_SUBMIT,
              function (FormEvent $event) use ($formModifier) {
                $channel1s = $event-&gt;getForm()-&gt;getData();
  
                $formModifier($event-&gt;getForm()-&gt;getParent(), $channel1s);
  
              }
  
              );
  
              $builder
              -&gt;add('agencies', EntityType::class, array(
              'class' =&gt; 'AppBundle:Agency',
              'property' =&gt; 'agent_name',
              'label' =&gt; 'label.agencies',
              'empty_value' =&gt; 'label.select_agency',
              'expanded' =&gt; false,
              'translation_domain' =&gt; 'UploadProfile',
              'multiple' =&gt; true,
              ));
          }
        public function configureOptions(OptionsResolver $resolver)
        {
          $resolver-&gt;setDefaults(array(
            'data_class' =&gt; 'DocumentBundle\Entity\UploadProfile',
            'em' =&gt; null,
  
          ));
        }
  
  
        public function getName()
        {
            return 'uploadprofile';
        }
      }
  </code></pre>
  
  <p>The error is for the $channel1s variable in this line:</p>
  
  <pre><code> $formModifier($event-&gt;getForm()-&gt;getParent(), $channel1s);
  </code></pre>
  
  <p>Channel1 has a ManyToMany Relationship to the Uploadprofile Entity.</p>
  
  <h2>Edit</h2>
  
  <p>Here's the dump:</p>
  
  <pre><code>ArrayCollection {#1828 ▼
    -elements: array:1 [▼
      0 =&gt; Channel1 {#1812 ▼
        #id: "C"
        #translationKey: "name.c"
        #name: "Consolidator"
        -uploadProfiles: PersistentCollection {#1895 ▼
          -snapshot: []
          -owner: Channel1 {#1812}
          -association: array:16 [ …16]
          -em: EntityManager {#456 …11}
          -backRefFieldName: "channel1s"
          -typeClass: ClassMetadata {#1810 …}
          -isDirty: false
          #collection: ArrayCollection {#1896 ▼
            -elements: []
          }
          #initialized: false
        }
        #channel3s: PersistentCollection {#1879 ▼
          -snapshot: []
          -owner: Channel1 {#1812}
          -association: array:15 [ …15]
          -em: EntityManager {#456 …11}
          -backRefFieldName: "channel1"
          -typeClass: ClassMetadata {#1748 …}
          -isDirty: false
          #collection: ArrayCollection {#1878 ▼
            -elements: []
          }
          #initialized: false
        }
      }
    ]
  }
  </code></pre>