2011-07-20 19 views
2

Symfony2でDoctrineを使用して画像ファイルをアップロードしようとしましたが、次のエラーが発生しています。:SQLSTATE [23000]:整合性制約違反:1048 'name'。ここ は私のEntityクラス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; 
    } 
} 

です。このエラーは、アップロードに関連していない関連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())); 

     } 


} 

答えて

5

で、

アップロードが動作しているようですが、それはの挿入です問題のあるデータベース

nameに指定した値がnullまたは空です。データベース・スキーマは、name列のNULL値を許可しません。

私は3つの可能な解決策を参照してください。

  • が$ nameプロパティにこの注釈を追加:@ORM\Column(type="string", length=255, nullable="true")
  • nameフィールドの値を提供し、あなたがフォームを送信する際に値を入力します。
  • あなたのオブジェクトの構築時にデフォルトnameを設定:

パブリック関数の__construct(){ ます$ this->名= 'foo' を。 }

+0

ありがとうございます。私の場合、あなたの2番目の選択肢が働いています。 –

関連する問題