2013-01-20 16 views
17

我正在使用Symfony2。 我有一個實體郵政有一個標題和一個圖片字段。Symfony 2 |修改具有文件(圖片)字段的對象時出現異常

我的問題:當我創建一個帖子,我有我的照片等等時,一切都很好。但是當我想修改它時,我有一個上傳文件的「圖片」字段的問題,Symfony想要文件類型,它有一個字符串(上傳文件的路徑):

The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File. 

我真的堅持了這個問題,真不知道如何解決它,任何幫助將不勝感激!非常感謝!

這是我的PostType.php(其在newAction()和modifiyAction()使用)和這可能導致問題(窗體/ PostType.php):

<?php 
namespace MyBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 

use MyBundle\Entity\Post; 

class PostType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
     ->add('title') 
     ->add('picture', 'file');//there is a problem here when I call the modifyAction() that calls the PostType file. 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'MyBundle\Entity\Post', 
     ); 
    } 

    public static function processImage(UploadedFile $uploaded_file, Post $post) 
    { 
     $path = 'pictures/blog/'; 
     //getClientOriginalName() => Returns the original file name. 
     $uploaded_file_info = pathinfo($uploaded_file->getClientOriginalName()); 
     $file_name = 
      "post_" . 
      $post->getTitle() . 
      "." . 
      $uploaded_file_info['extension'] 
      ; 

     $uploaded_file->move($path, $file_name); 

     return $file_name; 
    } 

    public function getName() 
    { 
     return 'form_post'; 
    } 
} 

這裏是我後實體實體/ post.php中):

<?php 

namespace MyBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

use Symfony\Component\Validator\Constraints as Assert; 

/** 
* MyBundle\Entity\Post 
* 
* @ORM\Table() 
* @ORM\Entity 
*/ 
class Post 
{ 
    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    * @Assert\Image(
    *  mimeTypesMessage = "Not valid.", 
    *  maxSize = "5M", 
    *  maxSizeMessage = "Too big." 
    *  ) 
    */ 
    private $picture; 

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

    //getters and setters 
    } 

這裏是我的newAction()控制器/ PostController.php每個正常工作與此功能

public function newAction() 
{ 
    $em = $this->getDoctrine()->getEntityManager(); 
    $post = new Post(); 
    $form = $this->createForm(new PostType, $post); 
    $post->setPicture(""); 
    $form->setData($post); 
    if ($this->getRequest()->getMethod() == 'POST') 
    { 
     $form->bindRequest($this->getRequest(), $post); 
     if ($form->isValid()) 
     { 
      $uploaded_file = $form['picture']->getData(); 
      if ($uploaded_file) 
      { 
       $picture = PostType::processImage($uploaded_file, $post); 
       $post->setPicture('pictures/blog/' . $picture); 
      } 
      $em->persist($post); 
      $em->flush(); 
      $this->get('session')->setFlash('succes', 'Post added.'); 

      return $this->redirect($this->generateUrl('MyBundle_post_show', array('id' => $post->getId()))); 
     } 
    } 

    return $this->render('MyBundle:Post:new.html.twig', array('form' => $form->createView())); 
} 

這裏是我的modifyAction()控制器/ PostController.php):有此功能存在問題

public function modifyAction($id) 
{ 
    $em = $this->getDoctrine()->getEntityManager(); 
    $post = $em->getRepository('MyBundle:Post')->find($id); 
    $form = $this->createForm(new PostType, $post);//THIS LINE CAUSES THE EXCEPTION 
    if ($this->getRequest()->getMethod() == 'POST') 
    { 
     $form->bindRequest($this->getRequest(), $post); 
     if ($form->isValid()) 
     { 
      $uploaded_file = $form['picture']->getData(); 
      if ($uploaded_file) 
      { 
       $picture = PostType::processImage($uploaded_file, $post); 
       $post->setPicture('pictures/blog/' . $picture); 
      } 
      $em->persist($post); 
      $em->flush(); 
      $this->get('session')->setFlash('succes', 'Modifications saved.'); 

      return $this->redirect($this->generateUrl('MyBundle_post_show', array('id' => $post->getId()))); 
     } 
    } 
    return $this->render('MyBundle:Post:modify.html.twig', array('form' => $form->createView(), 'post' => $post)); 
} 

回答

42

叫我解決了這個問題設置data_classnull如下:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
    ->add('title') 
    ->add('picture', 'file', array('data_class' => null) 
    ); 
} 
+0

你知道爲什麼這最終解決了問題嗎? – nbro

1

請在您的PostType.php中進行以下更改。

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
    ->add('title') 
    ->add('picture', 'file', array(
      'data_class' => 'Symfony\Component\HttpFoundation\File\File', 
      'property_path' => 'picture' 
     ) 
    ); 
} 
+0

嗨@Reveclaire請讓我知道,如果這有助於。如果它給出一些錯誤,你可以刪除'property_path'atrribute。否則保留它。 – OMG

+1

嗨@OMG!非常感謝您的回答。我修改了PostType。PHP如你所說(有和沒有'property_path',不幸的是我仍然有相同的錯誤)。 – Reveclair

+0

我解決了將data_class設置爲null的問題。感謝您讓我走上正軌。 – Reveclair

3

我建議你閱讀與Symfony和學說How to handle File Uploads with Doctrine,並強烈推薦給部分Lifecycle callbacks

文件上傳的文檔在簡短你通常的形式使用「文件」變量(請參閱文檔),您可以通過選項放置不同的標籤,然後在「圖片」字段中,您只需存儲文件的名稱,因爲當您需要src文件時,您可以調用getWebpath()方法。

->add('file', 'file', array('label' => 'Post Picture') 
); 

在樹枝模板

<img src="{{ asset(entity.webPath) }}" /> 
相關問題