SF2不会更新我的图像

时间:2013-12-23 08:32:31

标签: symfony

我对'HasLifecycleCallbacks'有疑问。当我尝试仅更新我的实体中的图像时,它不起作用(它不显示表单错误或php错误)。但是,如果我更新我的图像和字段(标题或文本),它正在工作!

没有其他错误,除了此功能外都可以。

namespace Acme\NewsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * News
 *
 * @ORM\Table(name="news")
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="Acme\NewsBundle\Entity\NewsRepository")
 */
class News
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=255, unique=true)
     */
    private $slug;

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

    /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;

    private $oldFile;

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

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

    public function __construct()
    {
        $this->created = new \Datetime;
    }

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

    /**
     * Set title
     *
     * @param string $title
     * @return News
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set slug
     *
     * @param string $slug
     * @return News
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * Set image
     *
     * @param string $image
     * @return News
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

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

    /**
     * Set file
     *
     * @param string $file
     * @return News
     */
    public function setFile($file)
    {
        $this->file = $file;

        return $this;
    }

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

    /**
     * Set text
     *
     * @param string $text
     * @return News
     */
    public function setText($text)
    {
        $this->text = $text;

        return $this;
    }

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

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return News
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

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

    // upload img
    public function getFullFilePath()
    {
        return null === $this->file ? null : $this->getUploadRootDir(). $this->file;
    }

    public function getUploadRootDir()
    {
        // the absolute directory path where uploaded documents should be saved
        return $this->getTmpUploadRootDir()."news/";
    }

    protected function getTmpUploadRootDir()
    {
        // the absolute directory path where uploaded documents should be saved
        return __DIR__ . '/../../../../web/up/';
    }

    public function getFilePath()
    {
        return null === $this->file ? null : "/web/up/news/". $this->file;
    }

    /**
     * @ORM\PreUpdate()
     */
    public function delOldFile() 
    {
        if (null === $this->file) {
            return;
        }

        if(file_exists($this->getUploadRootDir().$this->image)){
            $oldDir = $this->getUploadRootDir().$this->image;
            if(file_exists($oldDir)) {
                unlink($oldDir);
            }
        }
    }
    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function uploadFile() 
    {
        var_dump(true); die();
        // the file property can be empty if the field is not required
        if (null === $this->file) {
            return;
        }

        if(!is_dir($this->getUploadRootDir())){
            mkdir($this->getUploadRootDir());
        }

        $extension = pathinfo($this->file->getClientOriginalName(), PATHINFO_EXTENSION);
        $name = uniqid().'.'.$extension;

        $this->file->move($this->getUploadRootDir().'/', $name);

        // resize 145*145
        $cropImg   = $this->getUploadRootDir().'/'.$name;
        $cropDest  = $this->getUploadRootDir().'/'.$name;
        $largeur   = 145;
        $hauteur   = 145;
        $dimension = getimagesize($cropImg);
        $ratio     = $dimension[0] / $dimension[1];

        if($largeur == 0 && $hauteur == 0){ $largeur = $dimension[0]; $hauteur = $dimension[1]; }
        else if($hauteur == 0){ $hauteur = round($largeur / $ratio); }
        else if($largeur == 0){ $largeur = round($hauteur * $ratio); }
        if($dimension[0] > ($largeur/$hauteur)*$dimension[1] ){ $dimY = $hauteur; $dimX = round($hauteur*$dimension[0]/$dimension[1]); $decalX=($dimX-$largeur)/2; $decalY=0;}
        if($dimension[0] < ($largeur/$hauteur)*$dimension[1]){  $dimX = $largeur; $dimY = round($largeur*$dimension[1]/$dimension[0]); $decalY=($dimY-$hauteur)/2; $decalX=0;}
        if($dimension[0] == ($largeur/$hauteur)*$dimension[1]){ $dimX = $largeur; $dimY = $hauteur; $decalX=0; $decalY=0;}
        $miniature = imagecreatetruecolor ($largeur,$hauteur);
        if(in_array($extension,array('jpeg','jpg','JPG','JPEG'))){$file = imagecreatefromjpeg($cropImg); }
        elseif(in_array($extension,array('png','PNG'))){$file = imagecreatefrompng($cropImg); }
        elseif(in_array($extension,array('gif','GIF'))){$file = imagecreatefromgif($cropImg); }
        else{ return false; }
        imagecopyresampled($miniature,$file,-$decalX,-$decalY,0,0,$dimX,$dimY,$dimension[0],$dimension[1]);
        imagejpeg($miniature,$cropDest,90);

        $this->setImage($name);
    }

    /**
     * @ORM\PostPersist()
     */
    public function moveFile()
    {
        if (null === $this->file) {
            return;
        }
        if(!is_dir($this->getUploadRootDir())){
            mkdir($this->getUploadRootDir());
        }
    }

    /**
     * @ORM\PreRemove()
     */
    public function removeFile()
    {
        if ($this->getFullFilePath()) {
            unlink($this->getFullFilePath());
        }
    }
}

2 个答案:

答案 0 :(得分:2)

此问题已知错误,请查看文档:{​​{3}}

要解决此问题,该文档建议您使用“updatedAt”字段,每次持久保存实体时都会更新该字段,即使只更改了文件字段。

答案 1 :(得分:1)

检查您的实体表单类型是否已将映射(mapped选项设置为true)图像字段。

$builder->add('createdDateTime',null,
            array("mapped" => true, 
                  "required" => false,
                  "description" => "Thedatetime of creation")
);