0

我的实体有 3 个属性可以上传照片: photo1 photo2 photo3

如果我在没有文件的情况下提交这会取消设置所有旧数据文件,我会在第一时间坚持这一点,在编辑表单上

我的控制器(与新建/编辑相同,是个人资料表单):

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;
    }
}
4

1 回答 1

0

好的,不久前有这个“问题”,基本的想法是:

  • 从数据库中获取对象
  • 在表单绑定之前将当前文件存储到变量中(例如 $oldFile = $object->getFile())
  • 将传入的请求绑定到表单
  • if ( $object->getFile() === NULL ) $object->setFile($oldFile)

希望这可以帮助...

于 2012-10-07T11:16:54.240 回答