file - 执行上传时未定义的文件属性

标签 file upload properties symfony undefined

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

我关注了这个 tutorial我正在使用学说生命周期回调。

这是我的课:

<?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);
        }
    }
}

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

Notice: Undefined property: 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->file”更改为“$this->getFile()”,则会出现相同的错误,但它出现在 $file 的 getter 中。

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

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

有什么解决办法吗?

最佳答案

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

finally i fixed the problem. When using LifecycleCallbacks you do not have to call the upload method in your controller anymore otherwise it will cause an error.



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

关于file - 执行上传时未定义的文件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7371960/

相关文章:

python - Python 中有没有一种方法可以逐个标记地读取文本文件?

c - C 中的递归和链表

file - Dart:访问 project|web/目录之外的资源

c - 从 C 中的文件读取时覆盖结构数组

php - jQuery 文件上传插件 - 空文件上传结果

PHPUnit测试二进制数据上传

upload - KCFinder 是 CKFinder 的合法替代品吗

javascript - Reactjs 读取属性文件?

java - 这个属性文件和 "include"其他属性文件到底是如何工作的?

java - 无法使用 @PropertySource 将 .properties 文件注入(inject) Spring MVC 4.3