Ver Mensaje Individual
  #3 (permalink)  
Antiguo 20/12/2013, 06:11
jcrios_9
 
Fecha de Ingreso: noviembre-2013
Mensajes: 14
Antigüedad: 10 años, 6 meses
Puntos: 0
Respuesta: Subir Fotos con Symfony2

De momento he conseguido seguir la guia y poder almacenar las fotos, pero tengo un problemilla y es que no consigo que me almacene la foto usando el Id como nombre seguido de su extension. Si lo almaceno con su nombre original, si se almacena, pero quiero almacenarlo con el id como nombre para que cada foto tenga un nombre único. El campo Id es Auto y sigo la guia ,pero no consigo que funione. Os dejo el Picture.php por si me podeis ayudar:

Código PHP:
Ver original
  1. /**
  2.  * @ORM\Entity
  3.  * @ORM\HasLifecycleCallbacks
  4.  */
  5. class Picture
  6. {
  7.    
  8.     private $temp;
  9.    
  10.     /**
  11.      * @Assert\File(maxSize="6000000")
  12.      */
  13.     private $file;
  14.    
  15.      
  16.     /**
  17.      * @var integer
  18.      *
  19.      * @ORM\Column(type="integer", nullable=false)
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue(strategy="AUTO")
  22.      */
  23.     public $id;
  24.  
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      * @Assert\NotBlank
  28.      */
  29.     public $nombre;
  30.  
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=true)
  33.      */
  34.     public $path;
  35.    
  36.      /**
  37.      * @var \Perrobuscado
  38.      *
  39.      * @ORM\ManyToOne(targetEntity="Perrobuscado")
  40.      * @ORM\JoinColumns({
  41.      *   @ORM\JoinColumn(name="id_perrobuscado", referencedColumnName="id")
  42.      * })
  43.      */
  44.     private $idPerroBuscado;
  45.    
  46.    
  47.     /**
  48.      * Set idPerroBuscado
  49.      *
  50.      * @param \minsal\academicaBundle\Entity\Perrobuscado $idPerroBuscado
  51.      * @return Perrobuscado
  52.      */
  53.     public function setIdPerroBuscado(\minsal\academicaBundle\Entity\Perrobuscado $idPerroBuscado = null)
  54.     {
  55.         $this->idPerroBuscado = $idPerroBuscado;
  56.    
  57.         return $this;
  58.     }
  59.  
  60.     /**
  61.      * Get idPerroBuscado
  62.      *
  63.      * @return \minsal\academicaBundle\Entity\Perrobuscado
  64.      */
  65.     public function getIdPerroBuscado()
  66.     {
  67.         return $this->idPerroBuscado;
  68.     }
  69.  
  70.    
  71.  
  72.        protected function getUploadRootDir()
  73.     {
  74.         // la ruta absoluta del directorio donde se deben
  75.         // guardar los archivos cargados
  76.         return __DIR__.'/../../../../web/'.$this->getUploadDir();
  77.     }
  78.    
  79.         protected function getUploadDir()
  80.     {
  81.         // se deshace del __DIR__ para no meter la pata
  82.         // al mostrar el documento/imagen cargada en la vista.
  83.         return 'bundles/minsalacademica/imagenes/Pictures';
  84.     }
  85.  
  86.     /**
  87.      * Sets file.
  88.      *
  89.      * @param UploadedFile $file
  90.      */
  91.     public function setFile(UploadedFile $file = null)
  92.     {
  93.         $this->file = $file;
  94.         // check if we have an old image path
  95.         if (is_file($this->getAbsolutePath())) {
  96.             // store the old name to delete after the update
  97.             $this->temp = $this->getAbsolutePath();
  98.         } else {
  99.             $this->path = 'initial';
  100.         }
  101.     }
  102.    
  103.     /**
  104.      * Get file.
  105.      *
  106.      * @return UploadedFile
  107.      */
  108.     public function getFile()
  109.     {
  110.         return $this->file;
  111.     }
  112.  
  113.     /**
  114.      * @ORM\PrePersist()
  115.      * @ORM\PreUpdate()
  116.      */
  117. public function preUpload()
  118.     {
  119.  if (null !== $this->getFile()) {
  120.             $this->path = $this->getFile()->guessExtension();
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * @ORM\PostPersist()
  126.      * @ORM\PostUpdate()
  127.      */
  128.     public function upload()
  129.     {
  130.          if (null === $this->getFile()) {
  131.             return;
  132.         }
  133.  
  134.         // check if we have an old image
  135.         if (isset($this->temp)) {
  136.             // delete the old image
  137.             unlink($this->temp);
  138.             // clear the temp image path
  139.             $this->temp = null;
  140.         }
  141.  
  142.         // you must throw an exception here if the file cannot be moved
  143.         // so that the entity is not persisted to the database
  144.         // which the UploadedFile move() method does
  145.         $this->getFile()->move(
  146.             $this->getUploadRootDir(),
  147.             $this->id.'.'.$this->getFile()->guessExtension()
  148.         );
  149.  
  150.         $this->setFile(null);
  151.     }
  152.  
  153.    /**
  154.      * @ORM\PreRemove()
  155.      */
  156.     public function storeFilenameForRemove()
  157.     {
  158.         $this->temp = $this->getAbsolutePath();
  159.     }
  160.  
  161.     /**
  162.      * @ORM\PostRemove()
  163.      */
  164.     public function removeUpload()
  165.     {
  166.         if (isset($this->temp)) {
  167.             unlink($this->temp);
  168.         }
  169.     }
  170.  
  171.     public function getAbsolutePath()
  172.     {
  173.         return null === $this->path
  174.             ? null
  175.             : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
  176.     }
  177.    
  178.         public function getWebPath()
  179.     {
  180.         return null === $this->path
  181.             ? null
  182.             : $this->getUploadDir().'/'.$this->path;
  183.     }
  184.    
  185. }

Así no almacena ninguna imagen y en el atributo path, siempre almacena 'initial'.

Saludos.

Última edición por jcrios_9; 20/12/2013 a las 06:22