2011-07-20 29 views
2

我想在Symfony2中使用Doctrine上传图像文件,但出现以下错误:SQLSTATE [23000]:完整性约束违规:1048列'name'不能为空。 这里是我的实体类如何在Symfony2中使用Doctrine处理文件上传

<?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; 
    } 
} 

下面是相关的控制器类

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

     } 


} 

回答

5

此错误是不相关的上传,

上传似乎工作,但它在插入数据库谁有问题。

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

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

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

公共职能__construct(){$ 这 - >名称= '富'; }

+0

感谢朱尔斯。你的第二个选择是在我的情况下工作。 –

相关问题