亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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條經驗 獲得超4個贊

我認為您不應該在 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 關注
  • 149 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號