2012-08-24 29 views
0

我正在阅读并尝试应用this article about File Uploads,但出现了问题。有人说,这样的:我应该在哪里放上传功能 - Symfony2

if ($form->isValid()) { 
    $em = $this->getDoctrine()->getEntityManager(); 

    $document->upload(); 

    $em->persist($document); 
    $em->flush(); 

    $this->redirect(...); 
} 

应该在一个控制器去这里是upload功能

public function upload() 
{ 
    // the file property can be empty if the field is not required 
    if (null === $this->file) { 
     return; 
    } 

    // we use the original file name here but you should 
    // sanitize it at least to avoid any security issues 

    // move takes the target directory and then the target filename to move to 
    $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName()); 

    // set the path property to the filename where you'ved saved the file 
    $this->path = $this->file->getClientOriginalName(); 

    // clean up the file property as you won't need it anymore 
    $this->file = null; 
} 

的定义,但我应该在哪里放呢?我试图在控制器中,在调用它的动作之上,并且发生错误 - Fatal error: Call to undefined method...我也尝试将它放在不同的类和文件中,并添加其名称空间以供使用,但错误仍存在。 你能告诉我错误在哪里吗? :)

回答

1

如果你在代码仔细看,你会发现上载被称为文档对象的功能:

$document->upload(); 

所以,这是它应该去,在你的文档实体类

+0

有时我想知道我是否可以更愚蠢......非常感谢! – Faery