执行上传时未定义的文件属性
我在使用 Symfony 2 Upload 时遇到问题。我正在制作一个幻灯片管理器,我可以上传新幻灯片(带有图像文件),但我的类“幻灯片”的属性 $file 在上传过程中无法识别!
我遵循了这个教程,并且我正在使用学说生命周期回调。
这是我的班级:
<?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 行 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 对象。 如果我将 $this->file 的 var_dump 放入 setter 方法中,然后放入“exit;”,它也可以工作!
正如你所看到的,我的课程就像教程一样!
有什么解决办法吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是答案,对我有用! ;)
来源:
http://forum.symfony-project.org/viewtopic.php?f=23& t=35933
Here is the answer, works for me ! ;)
Source:
http://forum.symfony-project.org/viewtopic.php?f=23&t=35933
尝试在操作中删除
$entity->upload();
Try with a remove of
$entity->upload();
in your action将
unset($this->file);
更改为$this->file = null;
Change
unset($this->file);
by$this->file = null;