Symfony2 - 用于文件上载的实体中的未定义属性

时间:2014-06-16 10:08:43

标签: symfony file-upload

我在尝试上传文件时遇到以下错误,因为我在其他项目中使用了相同的代码而没有任何问题/错误,因此很奇怪。

我在这里缺少什么?

Notice: Undefined property: Acme\DemoBundle\Entity\Article::$file in /var/www/html/InsideFight/src/Acme/DempBundle/Entity/Article.php line 277

问题在于:

if (null !== $this->file) {

我的控制器中没有任何文件上传代码,它在实体中处理。

实体

public $file;


public function getUploadDir()
{
    return 'images/';
}

public function getUploadRootDir()
{
    return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}

public function getWebPath()
{
    return null === $this->image ? null : $this->getUploadDir() . '/' . $this->image;
}

public function getAbsolutePath()
{
    return null === $this->image ? null : $this->getUploadRootDir() . '/' . $this->image;
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    if (null !== $this->file) {
        $this->image = uniqid() . '.' . $this->file->guessExtension();
    }
}

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

    // If there is an error when moving the file, an exception will
    // be automatically thrown by move(). This will properly prevent
    // the entity from being persisted to the database on error
    $this->file->move($this->getUploadRootDir(), $this->image);

    unset($this->file);
}
/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if ($file = $this->getAbsolutePath()) {
        unlink($file);
    }
}

2 个答案:

答案 0 :(得分:2)

那是因为你unset($this->file);。将其更改为$this->file = null

答案 1 :(得分:0)

包含以下命名空间。

  use Symfony\Component\HttpFoundation\File\UploadedFile;

将文件变量设为私有并创建临时文件变量。

  private $file;
  private $tempFile

然后为$ file创建getter和setter方法。

public function getFile()
{
    return $this->file;
}

public function setFile(UploadedFile $file = null)
{
    $this->file = $file;

    if (isset($this->image)) {
        // store the old name to delete after the update
        $this->tempfile = $this->image;
        $this->image = null;
    } else {
        $this->image = 'initial';
    }
}

然后,修改preUpload和上传功能。

public function upload()
{
     if (null === $this->getFile()) {
        return;
    }

    // if there is an error when moving the file, an exception will
    // be automatically thrown by move(). This will properly prevent
    // the entity from being persisted to the database on error
    $this->getFile()->move($this->getUploadRootDir(), $this->image);

    // check if we have an old image
    if (isset($this->tempFile)) {
        // delete the old image
        unlink($this->getUploadRootDir() . '/' . $this->tempFile);

        // clear the temp image path
        $this->tempFile = null;
    }
    $this->file = null;
}



public function preUpload()
{
   if (null !== $this->getFile()) {
        // generate a unique name
        $filename = uniqid();


        $this->image = $filename . '.' . $this->getFile()->guessExtension();


    }
}