Symfony Type error: Argument passed must be an instance of entity or null, ArrayCollection given

1.9k views Asked by At

I get the following error:

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

Unfortunately I don't see my mistake in my FormType.

It looks like that:

    class SelectionType extends AbstractType {

      protected $em;

      public function __construct(\Doctrine\ORM\EntityManager $em)
      {
          $this->em = $em;
      }


      public function buildForm(FormBuilderInterface $builder, array $options)
        {


          $builder
          ->add('channel1s', EntityType::class, array(

            'class' => 'ReferentialBundle:Channel1',
            'property' => 'name',
            'label' => 'label.channel1s',
            'empty_value' => 'label.select_channel1s',
            'mapped' => false,
            'expanded' => false,
            'translation_domain' => 'UploadProfile',
            'multiple' => true,
            'required' => false,
          ));

          $formModifier = function (FormInterface $form, Channel1 $channel1s = null) {
            $channel3s = null === $channel1s ? array() : $channel1s->getChannel3s();

            $form
            ->add('channel3s', EntityType::class, array(
              'class' => 'ReferentialBundle:Channel3',
              'choices' => $channel3s,
            ));
          };
          $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) use ($formModifier) {
              $data = $event->getData();
              $formModifier($event->getForm(), $data->getChannel1s()[0]);
            }
          );
          $builder->get('channel1s')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
              $channel1s = $event->getForm()->getData();

              $formModifier($event->getForm()->getParent(), $channel1s);

            }

            );

            $builder
            ->add('agencies', EntityType::class, array(
            'class' => 'AppBundle:Agency',
            'property' => 'agent_name',
            'label' => 'label.agencies',
            'empty_value' => 'label.select_agency',
            'expanded' => false,
            'translation_domain' => 'UploadProfile',
            'multiple' => true,
            ));
        }
      public function configureOptions(OptionsResolver $resolver)
      {
        $resolver->setDefaults(array(
          'data_class' => 'DocumentBundle\Entity\UploadProfile',
          'em' => null,

        ));
      }


      public function getName()
      {
          return 'uploadprofile';
      }
    }

The error is for the $channel1s variable in this line:

 $formModifier($event->getForm()->getParent(), $channel1s);

Channel1 has a ManyToMany Relationship to the Uploadprofile Entity.

Edit

Here's the dump:

ArrayCollection {#1828 ▼
  -elements: array:1 [▼
    0 => 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
      }
    }
  ]
}
2

There are 2 answers

10
t-n-y On

You have to pass a Channel1 object to your formModifier, here you pass $event->getForm()->getData(), which is not a channel1 type

0
abderrahime sanadi On

For taking a object channel1 from arraycollection you must replace the code below :

channel1s = $event->getForm()->getData();

$formModifier($event->getForm()->getParent(), $channel1s);

by

channel1s = $event->getForm()->getData()->first();

$formModifier($event->getForm()->getParent(), $channel1s);