2012-10-07 23 views
0

我有我的實體3個atributes上傳照片:照片1照片2照片3Symfony2的文件上傳擦除舊數據

我堅持這個確定在第一關鍵時刻,編輯表單,如果我沒有文件這沒有設置所有的舊數據提交文件

我控制器(同樣以新建/編輯,是一個配置文件形式):

public function perfilAction() 
    { 
     $peticion = $this->getRequest(); 
     $em = $this->getDoctrine()->getEntityManager(); 

     $usuario = $this->get('security.context')->getToken()->getUser(); 
     $user = $em->getRepository('GarotaBundle:Garota')->find(1); 
     $formulario = $this->createForm(new GarotaType(), $usuario); 

     if ($peticion->getMethod() == 'POST') { 

      $formulario->bindRequest($peticion); 

      if ($formulario->isValid()) { 



       $usuario->subirFoto1(); 
       $usuario->subirFoto2();  
       $usuario->subirFoto3(); 
       /*echo "<pre>"; 
       var_dump($usuario); 

       echo "<br /><br /><br /><br /><br />"; 
       var_dump($user); 
       exit; 
       */ 
       if($usuario->getFoto1() == null) 
        $usuario->setFoto1($user->getFoto1()); 

       if($usuario->getFoto2() == null) 
        $usuario->setFoto2($user->getFoto2()); 

       if($usuario->getFoto3() == null) 
        $usuario->setFoto3($user->getFoto3()); 

       $em->persist($usuario); 
       $em->flush(); 

       $this->get('session')->setFlash('info', 
        'Dados atualizados com sucesso' 
       ); 
       return $this->redirect($this->generateUrl('perfil')); 
      } 
     } 

     return $this->render('GarotaBundle:Default:perfil.html.twig', array(
      'usuario' => $usuario, 
      'formulario' => $formulario->createView() 
     )); 
    } 

我的類:

<?php 

namespace Garotas\GarotaBundle\Entity; 

use Symfony\Component\Security\Core\User\UserInterface; 
use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert; 
use Symfony\Component\Validator\ExecutionContext; 
use Garotas\GarotaBundle\Util\Util; 

/** 
* Garotas\GarotaBundle\Entity\Garota 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Garotas\GarotaBundle\Entity\GarotaRepository") 
* @DoctrineAssert\UniqueEntity("login") 
*/ 
class Garota implements UserInterface 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string $login 
    * 
    * @ORM\Column(name="login", type="string", length=255) 
    */ 
    private $login; 

    /** 
    * @var string $password 
    * 
    * @ORM\Column(name="password", type="string", length=255) 
    */ 
    private $password; 

    /** 
    * @var string $password 
    * 
    * @ORM\Column(name="nome", type="string", length=255) 
    */ 
    private $nome; 

    /** 
    * @var string $password 
    * 
    * @ORM\Column(name="email", type="string", length=255) 
    */ 
    private $email; 

    /** 
    * @var string $salt 
    * 
    * @ORM\Column(name="salt", type="string", length=255) 
    */ 
    private $salt; 

    /** 
    * @var string $foto1 
    * 
    * @ORM\Column(name="foto1", type="string", length=255,nullable=true) 
    * @Assert\Image(maxSize = "500k") 
    */ 
    private $foto1; 

    /** 
    * @var string $foto2 
    * 
    * @ORM\Column(name="foto2", type="string", length=255,nullable=true) 
    * @Assert\Image(maxSize = "500k") 
    */ 
    private $foto2; 

    /** 
    * @var string $foto3 
    * 
    * @ORM\Column(name="foto3", type="string", length=255,nullable=true) 
    * @Assert\Image(maxSize = "500k") 
    */ 
    private $foto3; 

    /** 
    * @var \DateTime $data_cadastro 
    * 
    * @ORM\Column(name="data_cadastro", type="datetime") 
    */ 
    private $data_cadastro; 

    /** 
    * @var boolean $eliminada 
    * 
    * @ORM\Column(name="eliminada", type="boolean") 
    */ 
    private $eliminada; 

    /** 
    * @var boolean $eliminada 
    * 
    * @ORM\Column(name="termos", type="boolean") 
    */ 
    private $termos; 

    /** 
    * @var boolean $eliminada 
    * 
    * @ORM\Column(name="assinado", type="boolean") 
    */ 
    private $assinado; 

    /** 
    * @var boolean $eliminada 
    * 
    * @ORM\Column(name="categoria", type="integer") 
    */ 
    private $categoria; 

    function equals(\Symfony\Component\Security\Core\User\UserInterface $usuario) 
    { 
     return $this->getLogin() == $usuario->getLogin(); 
    } 

    /** 
    * Método requerido por la interfaz UserInterface 
    */ 
    function eraseCredentials() 
    { 
    } 

    /** 
    * Método requerido por la interfaz UserInterface 
    */ 
    function getRoles() 
    { 
     return array('ROLE_GAROTA'); 
    } 

    /** 
    * Método requerido por la interfaz UserInterface 
    */ 
    function getUsername() 
    { 
     return $this->getLogin(); 
    } 



    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set login 
    * 
    * @param string $login 
    * @return Garota 
    */ 
    public function setLogin($login) 
    { 
     $this->login = $login; 

     return $this; 
    } 

    public function __toString() 
    { 
     return $this->getNome(); 
    } 

    public function subirFoto1() 
    { 
     $directorioDestino = __DIR__.'/../../../../web/garotas'; 

     if (null === $this->foto1) { 
     return; 
    } 


     $nombreArchivoFoto = 'girleria-'.Util::getSlug($this->getNome()).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).'.'.$this->foto1->guessExtension(); 

     $this->foto1->move($directorioDestino, $nombreArchivoFoto); 

     $this->setFoto1($nombreArchivoFoto); 



    } 

    public function subirFoto2(){ 

     $directorioDestino = __DIR__.'/../../../../web/garotas'; 
      if (null === $this->foto2) { 
     return; 
    } 

     $nombreArchivoFoto = 'girleria-'.Util::getSlug($this->getNome()).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).'.'.$this->foto2->guessExtension(); 

     $this->foto2->move($directorioDestino, $nombreArchivoFoto); 

     $this->setFoto2($nombreArchivoFoto); 


    } 

    public function subirFoto3(){ 

     $directorioDestino = __DIR__.'/../../../../web/garotas'; 
     if (null === $this->foto3) { 
     return; 
    } 

     $nombreArchivoFoto = 'girleria-'.Util::getSlug($this->getNome()).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).'.'.$this->foto3->guessExtension(); 

     $this->foto3->move($directorioDestino, $nombreArchivoFoto); 

     $this->setFoto3($nombreArchivoFoto); 


    } 

    /** 
    * Get login 
    * 
    * @return string 
    */ 
    public function getLogin() 
    { 
     return $this->login; 
    } 

    public function setNome($nome) 
    { 
     $this->nome = $nome; 

     return $this; 
    } 

    public function getNome() 
    { 
     return $this->nome; 
    } 

    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set password 
    * 
    * @param string $password 
    * @return Garota 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    /** 
    * Get password 
    * 
    * @return string 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Set salt 
    * 
    * @param string $salt 
    * @return Garota 
    */ 
    public function setSalt($salt) 
    { 
     $this->salt = $salt; 

     return $this; 
    } 

    /** 
    * Get salt 
    * 
    * @return string 
    */ 
    public function getSalt() 
    { 
     return $this->salt; 
    } 

    /** 
    * Set foto1 
    * 
    * @param string $foto1 
    * @return Garota 
    */ 
    public function setFoto1($foto1) 
    { 
     $this->foto1 = $foto1; 

     return $this; 
    } 

    /** 
    * Get foto1 
    * 
    * @return string 
    */ 
    public function getFoto1() 
    { 
     return $this->foto1; 
    } 

    /** 
    * Set foto2 
    * 
    * @param string $foto2 
    * @return Garota 
    */ 
    public function setFoto2($foto2) 
    { 
     $this->foto2 = $foto2; 

     return $this; 
    } 

    /** 
    * Get foto2 
    * 
    * @return string 
    */ 
    public function getFoto2() 
    { 
     return $this->foto2; 
    } 

    /** 
    * Set foto3 
    * 
    * @param string $foto3 
    * @return Garota 
    */ 
    public function setFoto3($foto3) 
    { 
     $this->foto3 = $foto3; 

     return $this; 
    } 

    /** 
    * Get foto3 
    * 
    * @return string 
    */ 
    public function getFoto3() 
    { 
     return $this->foto3; 
    } 

    /** 
    * Set data_cadastro 
    * 
    * @param \DateTime $dataCadastro 
    * @return Garota 
    */ 
    public function setDataCadastro($dataCadastro) 
    { 
     $this->data_cadastro = $dataCadastro; 

     return $this; 
    } 

    /** 
    * Get data_cadastro 
    * 
    * @return \DateTime 
    */ 
    public function getDataCadastro() 
    { 
     return $this->data_cadastro; 
    } 

    /** 
    * Set eliminada 
    * 
    * @param boolean $eliminada 
    * @return Garota 
    */ 
    public function setEliminada($eliminada) 
    { 
     $this->eliminada = $eliminada; 

     return $this; 
    } 

    /** 
    * Get eliminada 
    * 
    * @return boolean 
    */ 
    public function getEliminada() 
    { 
     return $this->eliminada; 
    } 

    public function setTermos($termos) 
    { 
     $this->termos = $termos; 

     return $this; 
    } 

    public function getTermos() 
    { 
     return $this->termos; 
    } 

    public function setAssinado($assinado) 
    { 
     $this->assinado = $assinado; 

     return $this; 
    } 

    public function getAssinado() 
    { 
     return $this->assinado; 
    } 

    public function setCategoria($categoria) 
    { 
     $this->categoria = $categoria; 

     return $this; 
    } 

    public function getCategoria() 
    { 
     return $this->categoria; 
    } 
} 

回答

0

好,有這個「問題」而回的基本思路是:

  • 從DB
  • 保存當前的文件到取對象變量,以形成結合之前(例如$的oldfile = $對象 - >的GetFile())
  • 綁定傳入請求,以形成
  • if ($object->getFile() === NULL) $object->setFile($oldFile)

希望這有助於...

+0

我tryed這一點,但在這個時刻所取得的對象在文件上也爲null Oo,我做了這個轉儲 –

+0

你能100%確定該對象是否被正確保存?我沒有看到它(@JoinColumn)中沒有關係,所以我只能假設你正在爲'UploadFile'類型的對象添加對象?到目前爲止,我還沒有做過這樣的事情,所以我不確定可以這樣做。 –