dt97868 2019-01-31 09:37
浏览 6
已采纳

多文件上传symfony 4

I am trying to upload more than an image in the database. but my code uploads only the last one selected. here is the code:

/**
     * @Route("/property/add-media/{id}", name="addPhotoProperty")
     * @Method({"GET", "POST"})
     */
    public function addPhotoToProperty(Request $request, $id){
        $property = $this->getDoctrine()->getRepository(Property::class)->find($id);
        $media = new Media();

        $mediaForm = $this->createFormBuilder($media)
            ->add('pathtofile', FileType::class, array(
                'mapped' => false,
                'multiple' => true,
            ))
            ->add('isvideo', ChoiceType::class, [
                'choices' => [
                    'Video' => '1',
                    'Photo' => '0'
                ],
                'multiple' => false,
                'expanded' => true
            ])
            ->add('upload', SubmitType::class)
            ->getForm();

        $media->setProperty($property);
        $mediaForm->handleRequest($request);

        if ($mediaForm->isSubmitted() && $mediaForm->isValid()){
            $files = $mediaForm->get('pathtofile')->getData();
            //dd($files);

            foreach ($files as $file)
                {
                    $filename = md5(uniqid()).'.'.$file->guessExtension();
                    $file->move($this->getParameter('uploads'), $filename);
                    $media->setPathToFile($filename);
                    //dd($media);
                    $em = $this->getDoctrine()->getManager();
                    $em->persist($media);
                    $em->flush();
                }
        }

        return $this->render('Property/addPhotoProperty.html.twig', [
            'media' => $mediaForm->createView()
        ]);
    }

as you can see I am calling the object from class entity. the form of the file uploader accepts multiple files or images in this case.

  • 写回答

1条回答 默认 最新

  • doushaju4901 2019-01-31 09:48
    关注

    Your problem is in your loop. You are using the same Media entity and only change the PathToFile property. On first $em->flush(); you are creating a new entry, but since this is the same entity (AKA not a new one), Doctrine is doing an update.

    foreach ($files as $file)
    {
        $filename = md5(uniqid()).'.'.$file->guessExtension();
        $file->move($this->getParameter('uploads'), $filename);
        $media->setPathToFile($filename); // Same entity, that is being updated
    
        $em = $this->getDoctrine()->getManager();
        $em->persist($media);
        $em->flush();
    }
    

    I suggest you to create a new one in your loop. In example :

    $em = $this->getDoctrine()->getManager(); // You don't need to slow down your code and request the same entity manager on each iteration
    foreach ($files as $file)
    {
        $filename = md5(uniqid()).'.'.$file->guessExtension();
        $file->move($this->getParameter('uploads'), $filename);
    
        $NewMedia = new Media();             // new entity, that is being created
        $NewMedia->setProperty($property);
        $NewMedia->setPathToFile($filename);
    
        $em->persist($NewMedia);
    }
    $em->flush(); //Flush outside of the loop, Doctrine will perform all queries
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料