Php Symfony2-编辑上传的图像。

Php Symfony2-编辑上传的图像。,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,由于食谱中的这个例子,我已经能够在Symfony2和Doctrine中从MongoDB上传和检索图像: 但是,当我打开表单以更新图像(使用其他图像更改现有图像)时,会引发以下异常: 表单的视图数据应该是类的实例 Symfony\Component\HttpFoundation\File\File,但是的一个实例 类条令\MongoDB\GridFSFile。您可以通过设置 将“data_class”选项设置为null或通过添加 将类doctor\MongoDB\GridFSFile的实例转换为

由于食谱中的这个例子,我已经能够在Symfony2和Doctrine中从MongoDB上传和检索图像:

但是,当我打开表单以更新图像(使用其他图像更改现有图像)时,会引发以下异常:

表单的视图数据应该是类的实例 Symfony\Component\HttpFoundation\File\File,但是的一个实例 类条令\MongoDB\GridFSFile。您可以通过设置 将“data_class”选项设置为null或通过添加 将类doctor\MongoDB\GridFSFile的实例转换为 Symfony\Component\HttpFoundation\File\File的实例

我尝试将
'data\u class'
更改为
null
,与此线程的解决方案类似:但引发了另一个异常:

表单的视图数据应为标量、数组或 \ArrayAccess的实例,但是类的实例 条令\MongoDB\GridFSFile。通过设置 “数据类”选项添加到“条令\MongoDB\GridFSFile”或添加 视图转换器,用于转换类的实例 条令\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;
    }

}

我自己找到了解决办法!只需将
'data\u class'=>'Doctrine\MongoDB\GridFSFile'
设置如下:

$form = $this->createFormBuilder($carousel)
                ->add('title', 'text')
                ->add('file', 'file', array(
                    'data_class' => 'Doctrine\MongoDB\GridFSFile'
                ))
                ->add('content', 'textarea', array(
                    'attr' => array('cols' => '5', 'rows' => '10')
                ))
                ->add('save', 'submit')
                ->getForm();
$form = $this->createFormBuilder($carousel)
                ->add('title', 'text')
                ->add('file', 'file', array(
                    'data_class' => 'Doctrine\MongoDB\GridFSFile'
                ))
                ->add('content', 'textarea', array(
                    'attr' => array('cols' => '5', 'rows' => '10')
                ))
                ->add('save', 'submit')
                ->getForm();