Upload multiple symfony images 3

0

I have a problem: I have an entity Property and between its fields one called fotoUbicacion.La question is the following, I need that field to store the amount of images that the user uploads in the form. In the documentation of symfony 3.4 ( link they only show how to upload a file ... what modifications I would have to perform to achieve what I want. Here I leave my code (that way I can insert an image): Entity Property:

/**
 * @var string
 *
 * @ORM\Column(name="fotoUbicacion", type="string",nullable=true)
 */
private $fotoUbicacion;

    /**
 * Set fotoUbicacion
 *
 * @param string $fotoUbicacion
 *
 * @return Propiedad
 */
public function setFotoUbicacion($fotoUbicacion)
{
    $this->fotoUbicacion= $fotoUbicacion;

    return $this;
}

/**
 * Get fotoUbicacion
 *
 * @return string
 */
public function getFotoUbicacion()
{
    return $this->fotoUbicacion;
}
//Config.yml
    parameters:
locale: en
imagenes_directorio: '%kernel.project_dir%/web/uploads/imagenes_propiedades'
fotos_ubicacion_directorio: '%kernel.project_dir%/web/uploads/fotos_ubicacion'
//Controller
public function newAction(Request $request)
{
    $propiedad = new Propiedad();
    $form = $this->createForm('Assertsoft\InmobiliariaBundle\Form\PropiedadType', $propiedad);
    $form->handleRequest($request);

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


        $file2 = $propiedad->getFotoUbicacion();
        $fileName2 = $this->generateUniqueFileName().'.'.$file2->guessExtension();
        $file2->move($this->getParameter('fotos_ubicacion_directorio'),$fileName2);
        $propiedad->setFotoUbicacion($fileName2);

        $em = $this->getDoctrine()->getManager();
        $em->persist($propiedad);
        $em->flush();

        return $this->redirectToRoute('propiedad_show', array('id' => $propiedad->getId()));
    }

    return $this->render('@AssertsoftInmobiliaria/propiedad/new.html.twig', array(
        'propiedad' => $propiedad,
        'form' => $form->createView(),
    ));
}

    private function generateUniqueFileName()
{
    // md5() reduces the similarity of the file names generated by
    // uniqid(), which is based on timestamps
    return md5(uniqid());
}

//formtype
->add('fotoUbicacion',FileType::class,array('attr'=>array('onchange'=>'onChange(event)','multiple' => true)))
    
asked by Jesús Soto Mitjans 26.09.2018 в 00:11
source

1 answer

0
$image = new Image();
    $form = $this->createFormBuilder($image)
        ->add('full', FileType::class, array('label' => 'Imagen', 'multiple' => true))
        ->add('save', SubmitType::class, array('label' => 'Guardar'))
        ->getForm();
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $files = $image->getFull();
        foreach ($files as $file) {
            $image = new Image();
            $fileName = $fileUploader->upload($file);
            $image->setFull('uploads/images/' . $fileName);
            }
    }

It works perfect for me in this way. Full is the direction of the image.

    
answered by 16.11.2018 / 15:55
source