2014-10-11 64 views
2

我正在嘗試上傳基於VichUploaderBundle的文件。 我實現它,當這個錯誤:當通過VichUploaderBundle上傳文件時發生ContextErrorException

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Minn\AdsBundle\Entity\MotorsAdsFile::setFile() must be an instance of Symfony\Component\HttpFoundation\File\File, string given,... 

我知道問題出在方法setFile給定參數的()類。但是,我在github的文檔中發現了這個問題。

所以,這就是我所做的&我希望我能解決問題!捆綁我的檔案實體的

vich_uploader: 
    db_driver: orm # or mongodb or propel or phpcr 
    mappings: 
     motors_files: 
      uri_prefix:   /files/motors 
      upload_destination: %kernel.root_dir%/../web/files/motors 
      namer: vich_uploader.namer_origname 
      delete_on_remove: true 

定義的

配置

<?php 

namespace Minn\AdsBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\File; 
use Symfony\Component\Validator\Constraints as Assert; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* @ORM\Entity 
* @Vich\Uploadable 
*/ 
class MotorsAdsFile { 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    public $id; 

    /** 
    * @Assert\File(
    *  maxSize="5M", 
    *  mimeTypes={"image/png", "image/jpeg"} 
    *) 
    * @Vich\UploadableField(mapping="motors_files", fileNameProperty="file") 
    * note: This is not a mapped field of entity metadata, just a simple property. 
    * @var File $file 
    */ 
    protected $file; 

    /** 
    * @ORM\Column(type="string", length=255, name="name") 
    * @var string $name 
    */ 
    protected $name; 

    /** 
    * If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
    * of 'UploadedFile' is injected into this setter to trigger the update. If this 
    * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
    * must be able to accept an instance of 'File' as the bundle will inject one here 
    * during Doctrine hydration. 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file 
    */ 
    public function setFile(File $file) { 
     $this->file = $file; 
    } 

    /** 
    * @return File 
    */ 
    public function getFile() { 
     return $this->file; 
    } 

    /** 
    * @param string $name 
    */ 
    public function setName($name) { 
     $this->name = $name; 
    } 

    /** 
    * @return string 
    */ 
    public function getName() { 
     return $this->name; 
    } 

} 

嫩枝一部分的形式

<form action="{{ path('minn_ads_motors_test6') }}" {{ form_enctype(form) }} method="POST"> 
    {{ form_widget(form) }} 
    <div> 
     <input type="submit" value="do it with VICH" /> 
    </div> 
</form> 

我的行爲()在我的控制器

public function motorstest6Action(Request $request) { 
    $document = new MotorsAdsFile(); 
    $form = $this->createFormBuilder($document) 
      ->add('name') 
      ->add('file') 
      ->getForm(); 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($document); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('minn_ads_default_index')); 
    } 
    return $this->render('MinnAdsBundle:Motors:adddoc1.html.twig', array(
       'form' => $form->createView())); 
} 

回答

2

您的實體配置不正確。爲了使這個包能夠工作,實體必須有兩個字段來表示文件:一個用於存儲File對象(未由doctrine進行映射),另一個用於存儲其名稱(作爲字符串,由doctrine進行映射)。 您的實體只有第一個字段。

這裏是你如何定義它:

<?php 

namespace Minn\AdsBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\HttpFoundation\File\File; 
use Symfony\Component\Validator\Constraints as Assert; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* @ORM\Entity 
* @Vich\Uploadable 
*/ 
class MotorsAdsFile { 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    public $id; 

    /** 
    * @Assert\File(
    *  maxSize="5M", 
    *  mimeTypes={"image/png", "image/jpeg"} 
    *) 
    * @Vich\UploadableField(mapping="motors_files", fileNameProperty="filename") 
    * note: This is not a mapped field of entity metadata, just a simple property. 
    * @var File $file 
    */ 
    protected $file; 

    /** 
    * @ORM\Column(type="string", length=255, name="filename") 
    * @var string $filename 
    */ 
    protected $filename; 

    /** 
    * @ORM\Column(type="string", length=255, name="name") 
    * @var string $name 
    */ 
    protected $name; 

    /** 
    * If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
    * of 'UploadedFile' is injected into this setter to trigger the update. If this 
    * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
    * must be able to accept an instance of 'File' as the bundle will inject one here 
    * during Doctrine hydration. 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file 
    */ 
    public function setFile(File $file) { 
     $this->file = $file; 
    } 

    /** 
    * @return File 
    */ 
    public function getFile() { 
     return $this->file; 
    } 

    /** 
    * @param string $filenname 
    */ 
    public function setFilename($filename) { 
     $this->filename = $filename; 
    } 

    /** 
    * @return string 
    */ 
    public function getFilename() { 
     return $this->file; 
    } 

    /** 
    * @param string $name 
    */ 
    public function setName($name) { 
     $this->name = $name; 
    } 

    /** 
    * @return string 
    */ 
    public function getName() { 
     return $this->name; 
    } 
} 

注意新filename領域,在UploadableField註釋的filenameProperty選項的新值。

+0

幹得好的朋友! :) – 2014-10-11 12:14:09