错误与PostUpdate

时间:2014-02-05 03:06:12

标签: symfony doctrine-orm

当我一起添加 * * @ORM \ PostUpdate()和@ORM \ PostUpdate()时,我遇到此错误

FatalErrorException:错误:在非对象上调用成员函数guessExtension()

如果我删除 @ORM \ PostUpdate()它可以正常工作,但更新照片不起作用

    private $tempFilename;

/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
    // Si jamais il n'y a pas de fichier (champ facultatif)
    if (NULL === $this->image)
    {
        return;
    }

// Si on avait un ancien fichier, on le supprime
if (null !== $this->tempFilename) {
  $oldFile = $this->getUploadRootDir().'/'.$this->tempFilename;
  if (file_exists($oldFile)) {
    unlink($oldFile);
  }
}


// On garde le nom original du fichier de l'internaute
$name = $this->slug.'.'.$this->image->guessExtension();

// On déplace le fichier envoyé dans le répertoire de notrechoix
$this->image->move($this->getUploadRootDir(), $name);

// On sauvegarde le nom de fichier dans notre attribut $url
$this->image = $name;
$this->url =  $this->getUploadDir().'/'.$name; // retourne le chemin web du fichier qui lui peut être utilisé dans un 
                                               //template pour ajouter un lien vers le fichier uploadé

//$this->url = $this->getUploadRootDir().'/'.$name  :  retourne le chemin absolu du fichier

}


//...

1 个答案:

答案 0 :(得分:0)

尝试使用非严格的比较:

if (NULL == $this->image)
{
    return;
}

因为你的$ image变量可能被设置为空字符串(或类似的东西)。