symfony处理请求上传文件为空

我正在尝试在 Symfony 上上传多个文件,但是当提交表单时,表单图像字段返回一个像这样的空对象


object(Doctrine\Common\Collections\ArrayCollection)#1455 (1) {

  ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=>

  array(1) {

    [0]=>

    object(AdminBundle\Entity\ImageNew)#1717 (5) {

      ["nom":"AdminBundle\Entity\ImageNew":private]=>

      NULL

      ["path":"AdminBundle\Entity\ImageNew":private]=>

      NULL

      ["idimage":"AdminBundle\Entity\ImageNew":private]=>

      NULL

      ["categorie":"AdminBundle\Entity\ImageNew":private]=>

      NULL

      ["file":"AdminBundle\Entity\ImageNew":private]=>

      NULL

    }

  }

}

但是当我直接获取文件时,请求文件中的属性文件存在。我尝试通过访问请求中的属性来上传文件,它可以工作,但它仍然想通过 Symfony $form 请求处理程序上传文件。


这是我的控制器


 public function addColorAction(Request $request, Article $article)

    {

        $couleur = new Couleur();

        $form = $this->createForm('AdminBundle\Form\CouleurType', $couleur);

        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {


            $em = $this->getDoctrine()->getManager();


            $files = $couleur->getImages();


            echo "<pre>";

            var_dump($files); die;

            

            $imgs = $request->files->get("adminbundle_couleur")["images"];


            foreach ($imgs as $img) {

                $image = new ImageNew();

                $image->setFile($img["file"]);

                $image->upload();

                $couleur->addImage($image);


                $em->persist($image);

                $em->flush();

            }


            $color_art_dispo = new CouleurArticleDispo();

            $color_art_dispo->setEnStock(true);

            $color_art_dispo->setArticle($article);

            $color_art_dispo->setCouleur($couleur);

            

            $em->persist($couleur);

            $em->persist($color_art_dispo);

            $em->flush();


            return $this->redirectToRoute('article_index');

        }



隔江千里
浏览 106回答 2
2回答

守着一只汪

我认为您不应该在 ImageNewFileType 中添加mapped => false 选项。https://symfony.com/doc/current/reference/forms/types/form.html#mapped正如您在文档中看到的,写入对象时该字段将被忽略。

牧羊人nacy

错误发生在 ImageNewFileType 内部,因为属性 'mapped' => false,表单未在 ImageNew 实体的文件字段中设置上传的文件信息,所以我替换了此:class ImageNewFileType extends AbstractType{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* {@inheritdoc}&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function buildForm(FormBuilderInterface $builder, array $options)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $builder->add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file', FileType::class,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mapped' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attr' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'accept' => 'image/*',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}这样:class ImageNewFileType extends AbstractType{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* {@inheritdoc}&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function buildForm(FormBuilderInterface $builder, array $options)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $builder->add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file', FileType::class,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mapped' => true(or live this empty because by default it is true),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attr' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'accept' => 'image/*',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP