2012-08-24 30 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