I have a prob' with Symfony 2 Upload. I'm making a slideshow manager, and i can upload new slide (with an image file), but the property $file of my class "Slideshow" isn't recognized during upload !
I followed this tutorial and I'm using doctrine lifecycle callbacks.
Here my class:
<?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);
        }
    }
}
Now, when uploading, you can see my error:
Notice: Undefined property: Sybio\AppBundle\Entity\Slideshow::$file in /home/sybio/www/games/src/Sybio/AppBundle/Entity/Slideshow.php line 323
The line corresponding to the method:
/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    if (null !== $this->file) {
        $this->setPath(uniqid().'.'.$this->file->guessExtension());
    }
}
If I change "$this->file" for "$this->getFile()", I have the same error, but it appears in the getter of $file.
If i use the getter in action, it works and return an UploadedFile object. If i put a var_dump of $this->file in the setter method and then an "exit;", it works too !
And as you can see, my class is like the tutorial !!
Have any solution ?
Here is the answer, works for me ! ;)
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.
Source: http://forum.symfony-project.org/viewtopic.php?f=23&t=35933
Try with a remove of $entity->upload(); in your action
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With