0

我正在阅读并尝试应用这篇关于 File Uploads的文章,但是有一个问题。据说这是:

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

4

1 回答 1

1

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

$document->upload();

所以,这就是它应该去的地方,在你的 Document 实体类中

于 2012-08-24T08:59:07.510 回答