Download - Clase 14 doctrine - subir archivos
![Page 1: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/1.jpg)
Doctrine - Subir archivos
Realizada por:Christian Aquino |@cj_aquinoDiego Ramirez |@thedarsideofitGonzalo Alonso |@GonzaloAlonsoDDiego Barros |@Inmzombie
Para: Hydras C&S |@hydras_csBasada en Libro Symfony 2 en español Nacho Pacheco y The Book
![Page 2: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/2.jpg)
Cómo manejar archivos subidos con Doctrine
Puedes integrar la carga de archivos en el ciclo de vida de tu entidad (es decir, creación, actualización y eliminación). En este caso, ya que tu entidad es creada, actualizada y eliminada desde Doctrine, el proceso de carga y remoción de archivos se llevará a cabo de forma automática (sin necesidad de hacer nada en el controlador).
![Page 3: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/3.jpg)
Configuración básica
/** * @ORM\Column(type="string", length=255, nullable=true) */ public $path;
La propiedad ruta almacena la ruta relativa al archivo y se persiste en la base de datos.
![Page 4: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/4.jpg)
public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; }
El getAbsolutePath() es un método útil que devuelve la ruta absoluta al archivo
![Page 5: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/5.jpg)
public function getWebPath() { return null === $this->path ? null : $this->getUploadDir().'/'.$this->path; }
getWebPath() es un conveniente método que devuelve la ruta web, la cual se utiliza en una plantilla para enlazar el archivo cargado.
![Page 6: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/6.jpg)
protected function getUploadRootDir() { // la ruta absoluta del directorio donde se deben // guardar los archivos cargados return __DIR__.'/../../../../web/'.$this->getUploadDir(); }
protected function getUploadDir() { // se deshace del __DIR__ para no meter la pata // al mostrar el documento/imagen cargada en la vista. return 'uploads/documents'; }
![Page 7: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/7.jpg)
public function uploadAction(){ // ...
$form = $this->createFormBuilder($document) ->add('name') ->add('file') ->getForm();
// ...}
Para manejar el archivo real subido en el formulario, utiliza un campo file «virtual».
![Page 8: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/8.jpg)
// src/Acme/DemoBundle/Entity/Document.php
// ...class Document{ /** * @Assert\File(maxSize="6000000") */ public $file;
// ...}Debido a que estás utilizando la restricción File, Symfony2 automáticamente supone que el campo del formulario es una entrada para cargar un archivo.
![Page 9: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/9.jpg)
<h1>Subir archivo</h1>
<form action="#" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }}
<input type="submit" value="Upload Document" /></form>
Al escribir la plantilla, no olvides fijar el atributo enctype.
![Page 10: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/10.jpg)
Subir archivoif ($form->isValid()) { $em = $this->getDoctrine()->getManager();
$document->upload();
$em->persist($document); $em->flush(); $this->redirect(...);}
El método upload() tomará ventaja del objeto Symfony\Component\HttpFoundation\File\UploadedFile, el cual es lo que devuelve después de que se presenta un campo file:
![Page 11: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/11.jpg)
public function upload(){ if (null === $this->file) { return; }
$this->file->move( $this->getUploadRootDir(), $this->file->getClientOriginalName() );
$this->path = $this->file->getClientOriginalName()
$this->file = null;}
![Page 12: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/12.jpg)
Usando el ciclo de vida de las retrollamadas
¿Qué pasa si hay un problema al persistir la entidad?
/** * @ORM\Entity * @ORM\HasLifecycleCallbacks */class Document{}
![Page 13: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/13.jpg)
class Document{ /** * @ORM\PrePersist() * @ORM\PreUpdate() */ public function preUpload() { if (null !== $this->file) { // haz lo que quieras para generar un nombre único $filename = sha1(uniqid(mt_rand(), true)); $this->path = $filename.'.'.$this->file->guessExtension(); } }
![Page 14: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/14.jpg)
/** * @ORM\PostPersist() * @ORM\PostUpdate() */ public function upload() { if (null === $this->file) { return; }
// move() automáticamente envía una excepción. $this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file); }
![Page 15: Clase 14 doctrine - subir archivos](https://reader037.vdocumento.com/reader037/viewer/2022100605/559d25b11a28abc9518b457d/html5/thumbnails/15.jpg)
/** * @ORM\PostRemove() */ public function removeUpload() { if ($file = $this->getAbsolutePath()) { unlink($file); } }}
La clase ahora hace todo lo que necesitas: genera un nombre de archivo único antes de persistirlo, mueve el archivo después de persistirlo y elimina el archivo si la entidad es eliminada.