Symfony2-用于文件上载的实体中未定义的属性
我在尝试上传文件时遇到以下错误,这很奇怪,因为我在其他项目中使用了相同的代码,没有任何问题/错误 我错过了什么Symfony2-用于文件上载的实体中未定义的属性,symfony,file-upload,Symfony,File Upload,我在尝试上传文件时遇到以下错误,这很奇怪,因为我在其他项目中使用了相同的代码,没有任何问题/错误 我错过了什么 Notice: Undefined property: Acme\DemoBundle\Entity\Article::$file in /var/www/html/InsideFight/src/Acme/DempBundle/Entity/Article.php line 277 问题是: if (null !== $this->file) { 我的控制器中没有任何文件上
Notice: Undefined property: Acme\DemoBundle\Entity\Article::$file in /var/www/html/InsideFight/src/Acme/DempBundle/Entity/Article.php line 277
问题是:
if (null !== $this->file) {
我的控制器中没有任何文件上载代码,它正在实体中处理
实体
public $file;
public function getUploadDir()
{
return 'images/';
}
public function getUploadRootDir()
{
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
public function getWebPath()
{
return null === $this->image ? null : $this->getUploadDir() . '/' . $this->image;
}
public function getAbsolutePath()
{
return null === $this->image ? null : $this->getUploadRootDir() . '/' . $this->image;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
$this->image = uniqid() . '.' . $this->file->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
// If there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->image);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
包括以下命名空间
use Symfony\Component\HttpFoundation\File\UploadedFile;
将文件变量设为私有并创建临时文件变量
private $file;
private $tempFile
然后为$file创建getter和setter方法
public function getFile()
{
return $this->file;
}
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
if (isset($this->image)) {
// store the old name to delete after the update
$this->tempfile = $this->image;
$this->image = null;
} else {
$this->image = 'initial';
}
}
然后,修改预上传和上传功能
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->image);
// check if we have an old image
if (isset($this->tempFile)) {
// delete the old image
unlink($this->getUploadRootDir() . '/' . $this->tempFile);
// clear the temp image path
$this->tempFile = null;
}
$this->file = null;
}
public function preUpload()
{
if (null !== $this->getFile()) {
// generate a unique name
$filename = uniqid();
$this->image = $filename . '.' . $this->getFile()->guessExtension();
}
}
这是因为你做了
unset($this->file)代码>。将其更改为$this->file=null
确定尝试此操作并获得此错误:FatalErrorException:错误:调用/var/www/html/Symfony/src/Acme/DemoBundle/Entity/Article.php第334行
第334行是$this->file->move($this->getUploadRootDir(),$this->image)代码>将该行更改为$this->getFile->move($this->getUploadRootDir(),$this->image);非常感谢。这是可行的,我不知道为什么以前不可行,因为我之前在其他项目中使用了相同的代码,而且效果很好。你能解释一下unset和$this->file=null的区别吗???unset
remove属性完全从对象中删除,这样它就不被定义了$this->file=null
只需将其设置为null
。对,但这正是我在前面代码中使用的行,它可以工作。与unset相同的行。这是Symfony2新版本的新功能吗?这只是注意
。可能您在以前的应用程序上的php错误级别不同(例如:error\u reporting(E\u ALL&~E\u NOTICE);
)。唯一不同的是Symfony2版本(2.4和2.5)以及我在以前的项目中没有使用stof和Gedmo(stofdoctrineextensions)来处理slug。使用SonataAdminBundle和那个通知阻止了我的新帖子的创建。令人沮丧的!