Symfony 2 | Исключение формы при изменении объекта с файлом (картиной)

Я использую Symfony2. У меня есть объект Post, который имеет заголовок и поле изображения.

Моя проблема: Все хорошо, когда я создаю сообщение, у меня есть изображение и т.д. Но когда я хочу его изменить, у меня возникла проблема с полем "картинка", которое загружено file, Symfony хочет тип файла и имеет строку (путь к загруженному файлу):

The form 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()) и может вызвать проблему (Form/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 (Entity/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() (Controller/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() (Controller/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));
}

Ответы

Ответ 1

Я решил установить проблему data_class на null следующим образом:

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

Ответ 2

Я бы порекомендовал вам прочитать документацию по загрузке файлов с помощью Symfony и Doctrine Как обрабатывать загрузки файлов с помощью Doctrine и сильная рекомендация для части Обратные вызовы жизненного цикла

Вкратце вы обычно в форме используете переменную "файл" (см. документацию), вы можете поместить другую метку через параметры, а затем в поле "изображение" просто сохраните имя файла, потому что когда вам нужен файл src, вы можете просто вызвать метод getWebpath().

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

чтобы вызвать ваш шаблон ветки.

<img src="{{ asset(entity.webPath) }}" />

Ответ 3

Пожалуйста, сделайте следующее изменение в 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'
        )
    );
}