【问题标题】:Where should I put the upload function - Symfony2我应该把上传功能放在哪里 - Symfony2
【发布时间】:2012-08-24 08:51:22
【问题描述】:

我正在阅读并尝试申请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 添加其命名空间,但错误仍然存​​在。 你能告诉我错误在哪里吗? :)

【问题讨论】:

    标签: symfony


    【解决方案1】:

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

    $document->upload();
    

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

    【讨论】:

    • 有时我想知道我是否可以更愚蠢...非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 2013-10-27
    • 2011-03-28
    • 2020-09-30
    • 2016-08-25
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多