如何在Symfony2中使用Doctrine处理文件上载

时间:2011-07-20 06:11:45

标签: image-processing doctrine-orm symfony

我正在尝试使用Sytrfony2中的Doctrine上传图像文件但是我收到以下错误: SQLSTATE [23000]:完整性约束违规:1048列'name'不能为null 。 这是我的实体类

<?php
// src/Acme/DemoBundle/Entity/Document.php
namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="document")
 */
class Document
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank
     */
    public $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $path;

    /**
     * @Assert\File(maxSize="6000000")
     */
    public $file;

    public function getUploadRootDir()
    {
        return '/uploads';
    }

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

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

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

    /**
     * Set path
     *
     * @param string $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }

    /**
     * Get path
     *
     * @return string $path
     */
    public function getPath()
    {
        return $this->path;
    }


    /**
     * @ORM\PrePersist()
     */
    public function preUpload()
    {
        if ($this->file) {
            $this->setPath($this->file->guessExtension());
        }
    }

    /**
     * @ORM\PostPersist()
     */
    public function upload()
    {
        if (!$this->file) {
            return;
        }

        try{
            $this->file->move($this->getUploadRootDir(), $this->id.'.'.$this->file->guessExtension());
        }
        catch(FilePermissionException $e)
        {
            return false;
        }

        catch(\Exception $e)
        {
            throw new \Exception($e->getMessage());
        }
        unset($this->file);
    }

    /**
     * @ORM\PreRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getFullPath()) {
            unlink($file);
        }
    }

    public function getFullPath()
    {
        return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
    }
}

这是相关的Controller类

<?php

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Acme\DemoBundle\Form\ContactForm;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use Acme\DemoBundle\Entity\Document;

class FileUploadController extends Controller
{
    protected $file;
    protected $document;

    public function indexAction()
    {
        $this->document = new Document();


        $form = $this->get('form.factory')
             ->createBuilder('form')
             ->add('name','text')
             ->add('file','file')
             ->getForm();

        $request = $this->get('request');

        if ($request->getMethod() === 'POST') {
            $form->bindRequest($request);
            if ($form->isValid()) {

                $em = $this->get('doctrine.orm.entity_manager');

                $this->document->upload();


                $em->persist($this->document);
                $em->flush();


                $this->get('session')->setFlash('notice', 'The file is uploaded!');
            }
        }

        return $this->render('AcmeDemoBundle:FileUpload:index.html.twig',
            array("form"=>$form->createView()));

        }


}

1 个答案:

答案 0 :(得分:5)

此错误与上传无关,

上传似乎有效,但是数据库中的插入有问题。

您为name提供的值为null或为空。您的数据库架构不允许name列的空值。

我看到了3种可能的解决方案:

  • 将此注释添加到$ name属性:@ORM\Column(type="string", length=255, nullable="true")
  • name字段提供值,请在提交表单时输入值。
  • 在构建对象时设置默认name
  

public function __construct(){       $ this-&gt; name ='foo';   }