Symfony Type错误,例如ArrayCollection而不是Entity实例

时间:2017-08-25 12:30:54

标签: symfony typeerror instanceof arraycollection

我经常收到错误消息:  类型错误:传递给DocumentBundle \ Form \ SelectionType :: DocumentBundle \ Form {closure}()的参数2必须是AppBundle \ Entity \ Channel1的实例或null,Doctrine \ Common \ Collections \ ArrayCollection的实例,调用在第91行的/srv/http/sp/src/DocumentBundle/Form/SelectionType.php

但我还没看到我的错误在哪里。我是php&的新手symfony,请原谅我的不知情。 以下是FormType代码,其中发生错误:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
$builder
    ->add('channel1s', EntityType::class, array(
      // 'data' => $channel1s,
      'class' => 'AppBundle: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' => 'AppBundle:Channel3',
        'property' => 'name',
        'label' => 'label.channel3s',
        'empty_value' => 'label.select_channel3s',
        'mapped' => false,
        'expanded' => false,
        'translation_domain' => 'UploadProfile',
        'multiple' => true,

      ));
    };
  $builder->addEventListener(
      FormEvents::PRE_SET_DATA,
      function (FormEvent $event) use ($formModifier) {
        $data = $event->getData();
        $formModifier($event->getForm(), $data->getChannel1s());
      }
    );

    $builder->get('channel1s')->addEventListener(
      FormEvents::POST_SUBMIT,
      function (FormEvent $event) use ($formModifier) {
        $channel1s = $event->getForm()->getData();
        dump($channel1s);
        $formModifier($event->getForm()->getParent(), $channel1s);
      }
      );
}

以下是我的实体:(我正在尝试创建一个上传文档的表单,称为“uploadprofiles”,我不确定您需要看哪个实体才能帮助我) Uploadprofile看起来像那样(只有一些部分):

 /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Channel1", inversedBy="uploadProfiles", cascade={"persist"})
     * @ORM\JoinTable(name="document_uploadprofile_channel1s",
     *   joinColumns={@ORM\JoinColumn(name="uploadprofile_id", referencedColumnName="id")},
     *   inverseJoinColumns={@ORM\JoinColumn(name="channel1_id", referencedColumnName="id")})
     * @var \AppBundle\Entity\Channel1
     **/
    private $channel1s;

        /**
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Channel3", inversedBy="uploadProfiles", cascade={"persist"})
         *  @ORM\JoinTable(name="document_uploadprofile_channel3s",
         *   joinColumns={@ORM\JoinColumn(name="uploadprofile_id", referencedColumnName="id")},
         *   inverseJoinColumns={@ORM\JoinColumn(name="channel3_id", referencedColumnName="id")})
         * @var \AppBundle\Entity\Channel3
         **/
    private $channel3s;
  /**
     * Constructor
     */
    public function __construct()
    {
        $this->agencies = new \Doctrine\Common\Collections\ArrayCollection();
        $this->channel3s = new \Doctrine\Common\Collections\ArrayCollection();
        $this->channel1s = new \Doctrine\Common\Collections\ArrayCollection();
        $this->locked = false;
        $this->archived = false;
    }

然后是这两个人的Getters,Setters,'Adders'和'Removers'。

是否有人看到错误或需要更多信息以帮助我?

1 个答案:

答案 0 :(得分:0)

你试过吗?

'expanded' => true