Symfony Type错误:传递的参数必须是实体的实例或null,给定

时间:2017-08-28 14:25:34

标签: symfony typeerror instanceof arraycollection

我收到以下错误:

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

不幸的是我没有在FormType中看到我的错误,所以我很乐意为您提供任何帮助!

看起来像是:

    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';
      }
    }

错误在于此行中的$ channel1s变量:

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

Channel1与Uploadprofile实体有一个ManyToMany关系,如果这有助于识别我的错误! 提前谢谢!

修改 这是转储:

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 个答案:

答案 0 :(得分:0)

你必须将Channel1对象传递给formModifier,在这里传递$event->getForm()->getData(),这不是channel1类型

答案 1 :(得分:0)

要从arraycollection中获取对象channel1,您必须替换以下代码:

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

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

通过

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

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