我已經能夠上傳和學說感謝檢索的Symfony2從MongoDB的影像傳輸到本例中的食譜:http://symfony.com/doc/current/cookbook/doctrine/file_uploads.htmlSymfony2 - 編輯上傳的圖像。
但是當我打開窗體,以更新的圖像(改變與其它圖像現有的),這引發異常:
表單的視圖數據預計將類 的Symfony \分量\ HttpFoundation的實例\文件\文件,但 類學說\ MongoDB的\ GridFSFile的一個實例。您可以通過將 「data_class」選項設置爲null或通過添加視圖轉換器來避免此錯誤,該視圖轉換器 將Doctrine \ MongoDB \ GridFSFile類的實例轉換爲Symfony \ Component \ HttpFoundation \ File \ File的 實例。
我試圖'data_class'
改變null
這樣線程的溶液:Symfony 2 | Form exception when modifying an object that has a file(picture) field但另一引發異常:
表單的視圖數據的類型應爲標量,數組或一個 的\ ArrayAccess的實例,但是是Doctrine \ MongoDB \ GridFSFile類的一個實例。您可以通過將 「data_class」選項設置爲「Doctrine \ MongoDB \ GridFSFile」或添加一個將類 Doctrine \ MongoDB \ GridFSFile的實例轉換爲標量,數組或實例 \ ArrayAccess接口。
這裏是我的文檔類:
<?php
namespace ChildCare\AdminBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @MongoDB\Document
*/
class Carousel {
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\String
*/
protected $title;
/**
* @MongoDB\String
*/
protected $path;
public function getAbsolutePath() {
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath() {
return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir() {
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir() {
return 'bundles/childcare/img/carousel';
}
/**
* @MongoDB\File
*/
protected $file;
/**
* @MongoDB\String
*/
protected $content;
/**
* @MongoDB\Date
*/
protected $date;
/**
* Get id
*
* @return id $id
*/
public function getId() {
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return self
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string $title
*/
public function getTitle() {
return $this->title;
}
/**
* Set path
*
* @param string $path
* @return self
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string $path
*/
public function getPath() {
return $this->path;
}
/**
* Set file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null) {
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile() {
return $this->file;
}
/**
* Set content
*
* @param string $content
* @return self
*/
public function setContent($content) {
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string $content
*/
public function getContent() {
return $this->content;
}
/**
* Set date
*
* @param date $date
* @return self
*/
public function setDate($date) {
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return date $date
*/
public function getDate() {
return $this->date;
}
public function upload() {
if (null === $this->getFile()) {
return;
}
$this->getFile()->move(
$this->getUploadRootDir(), $this->getFile()->getClientOriginalName()
);
$this->path = $this->getUploadDir() . '/' . $this->getFile()->getClientOriginalName();
$this->file = null;
}
}
而且這是在我的控制器行動的一部分:
$form = $this->createFormBuilder($carousel)
->add('title', 'text')
->add('file', 'file', array(
'data_class' => null
))
->add('content', 'textarea', array(
'attr' => array('cols' => '5', 'rows' => '10')
))
->add('save', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$carousel->setTitle($form['title']->getData());
$carousel->setContent($form['content']->getData());
$carousel->setDate(new \DateTime());
$carousel->upload();
if ($id == 'create') {
$dm->persist($carousel);
$dm->flush();
return $this->redirect($this->generateUrl('admin_page_info'));
} else {
$dm->flush();
return $this->redirect($this->generateUrl('admin_page_info'));
}
}
return $this->render('ChildCareAdminBundle:WebInfo:editCarousel.html.twig', array(
'form' => $form->createView()));