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

Syb*_*bio 5 upload properties file undefined symfony

我有一个问题'与Symfony 2上传.我正在制作一个幻灯片管理器,我可以上传新的幻灯片(带有图像文件),但上传期间无法识别我的类"幻灯片"的属性$文件!

我遵循了本教程,我正在使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

对应方法的行:

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

如果我为"$ this-> getFile()"更改"$ this-> file",我有同样的错误,但它出现在$ file的getter中.

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

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

有解决方案吗?

小智 9

这是答案,适合我!;)

最后我解决了这个问题.使用LifecycleCallbacks时,您不必再在控制器中调用upload方法,否则会导致错误.

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