为了账号安全,请及时绑定邮箱和手机立即绑定

symfony处理请求上传文件为空

symfony处理请求上传文件为空

PHP
隔江千里 2023-10-15 17:27:36
我正在尝试在 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');        }
查看完整描述

2 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

我认为您不应该在 ImageNewFileType 中添加mapped => false 选项。

https://symfony.com/doc/current/reference/forms/types/form.html#mapped

正如您在文档中看到的,写入对象时该字段将被忽略。


查看完整回答
反对 回复 2023-10-15
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

错误发生在 ImageNewFileType 内部,因为属性 'mapped' => false,表单未在 ImageNew 实体的文件字段中设置上传的文件信息,所以我替换了此:


class ImageNewFileType extends AbstractType

{

    /**

     * {@inheritdoc}

     */

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

    {

        $builder->add(

            'file', FileType::class, 

            [

                'mapped' => false,

                'required' => false,

                'attr' => array(

                    'accept' => 'image/*',

                )

            ]

        );

    }

}

这样:


class ImageNewFileType extends AbstractType

{

    /**

     * {@inheritdoc}

     */

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

    {

        $builder->add(

            'file', FileType::class, 

            [

                'mapped' => true(or live this empty because by default it is true),

                'required' => false,

                'attr' => array(

                    'accept' => 'image/*',

                )

            ]

        );

    }

}


查看完整回答
反对 回复 2023-10-15
  • 2 回答
  • 0 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信