我应该把上传功能放在哪里 - Symfony2

时间:2012-08-24 08:51:22

标签: symfony

我正在阅读并尝试应用this article about File Uploads,但是存在问题。据说:

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

    $document->upload();

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

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

应该进入Controller,这里是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...我还尝试将其放在不同的类和文件中并使用use添加其命名空间,但错误仍然存​​在。 你能告诉我哪里出错吗? :)

1 个答案:

答案 0 :(得分:1)

如果仔细查看代码,您会看到上传是作为文档对象的函数调用的:

$document->upload();

所以,它应该在你的Document实体类