I have a problem downloading Symfony 2. I am creating a slide show manager and I can upload a new slide (with an image file), but the class property file of my Slideshow class is not recognized at boot time!
I followed this tutorial and I use doctrine lifecycle callbacks.
Here is 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;
class Slideshow implements Translatable
{
protected $id;
protected $file;
protected $path;
private $locale;
public function getId()
{
return $this->id;
}
public function setFile($file)
{
$this->file = $file;
}
public function getFile()
{
return $this->file;
}
public function setPath($path)
{
$this->path = $path;
}
public function getPath()
{
return $this->path;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
public function getUploadRootDir()
{
return __DIR__.'/../../../../web'.$this->getUploadDir();
}
public function getUploadDir()
{
return '/uploads/slideshow/'.$this->createdAt->format("Y/m/d");
}
public function preUpload()
{
if (null !== $this->file) {
$this->setPath(uniqid().'.'.$this->file->guessExtension());
}
}
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);
}
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
Now, at boot, you can see my error:
Note: Undefined property: Sybio \ AppBundle \ Entity \ Slideshow :: $ file in /home/sybio/www/games/src/Sybio/AppBundle/Entity/Slideshow.php 323
Line corresponding to the method:
public function preUpload()
{
if (null !== $this->file) {
$this->setPath(uniqid().'.'.$this->file->guessExtension());
}
}
If I change "$ this-> file" to "$ this-> getFile ()", I have the same error, but it appears in the getter of the $ file.
getter , UploadedFile.
var_dump $this- > setter, "exit;", !
, !
- ?