Symfony2 - 编辑上传的图像。

时间:2014-05-02 07:45:30

标签: php symfony doctrine-orm doctrine

由于Cookbook中的这个示例,我已经能够使用Doctrine从Symfony2中的MongoDB上传和检索图像:http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

但是当我打开表单来更新图像(用另一个图像更改现有图像)时,会引发此异常:

  

表单的视图数据应该是类的实例   Symfony \ Component \ HttpFoundation \ File \ File,但是是一个实例   class Doctrine \ 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的实例,但是是类的实例   学说\ 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;
    }

}

这是我控制器中Action的一部分:

$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()));

1 个答案:

答案 0 :(得分:2)

我自己发现的解决方案!只需将'data_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();