mike
mike

Reputation: 63

multiple file upload 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.

Upvotes: 0

Views: 4588

Answers (1)

Cid
Cid

Reputation: 15247

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

Upvotes: 2

Related Questions