执行上载时未定义的文件属性

时间:2011-09-10 13:20:54

标签: file upload properties symfony undefined

我对Symfony 2上传有疑问。我正在制作幻灯片管理器,我可以上传新的幻灯片(带有图像文件),但上传期间无法识别我的类“幻灯片”的属性$文件!

我遵循了这个tutorial,我正在使用doctrine生命周期回调。

我的班级:

<?php
namespace Sybio\AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\MinLength;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Entity(repositoryClass="Sybio\AppBundle\Entity\Repository\SlideshowRepository")
 * @ORM\Table(name="slideshow")
 * @ORM\HasLifecycleCallbacks
 */
class Slideshow implements Translatable
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Assert\File(maxSize="1048576")
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;

    /**
     * @Gedmo\Locale
     */
    private $locale;

    //Other properties not shown in this paste...

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

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

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

    /**
     * Set path
     *
     * @param string $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }

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

    /**
     * Set local
     * 
     * @param string $locale
     */
    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }

    // Others getter and setter methods not shown in this paste ...

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

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

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

    /**
     * getUploadDir of slideshow
     */
    public function getUploadDir()
    {
        return '/uploads/slideshow/'.$this->createdAt->format("Y/m/d");
    }

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

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null !== $this->file) {
            $this->setPath(uniqid().'.'.$this->file->guessExtension());
        }

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

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

        $this->file->move($this->getUploadRootDir(), $this->path);

        unset($this->file);
    }

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

现在,上传时,您可以看到我的错误:

  

注意:未定义的属性:Sybio \ AppBundle \ Entity \ Slideshow :: $ file in   /home/sybio/www/games/src/Sybio/AppBundle/Entity/Slideshow.php line   323

与方法对应的行:

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

如果我为“$ this-&gt; getFile()”更改“$ this-&gt; file”,我会遇到同样的错误,但它出现在$ file的getter中。

如果我使用getter,它会工作并返回一个UploadedFile对象。 如果我在setter方法中放入一个$ this-&gt;文件的var_dump,然后是“exit;”,它也可以工作!

正如你所看到的,我的课程就像教程一样!!

有解决方案吗?

3 个答案:

答案 0 :(得分:9)

这是答案,对我有用! ;)

  

最后我解决了这个问题。使用LifecycleCallbacks时,您没有   必须再在你的控制器中调用上传方法,否则它   会导致错误。

来源: http://forum.symfony-project.org/viewtopic.php?f=23&t=35933

答案 1 :(得分:3)

尝试在操作中删除$entity->upload();

答案 2 :(得分:0)

unset($this->file);

更改$this->file = null;