2012-07-28 1 views
1

은 엔티티와 파일 업로드를 관리하려고 노력하는,하지만 난이 오류를 얻을 :Symfony forms. 파일 업로드

namespace BS\BackstretcherBundle\Entity; 

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

/** 
* MB\MyBundle\Entity\Items 
* 
* @ORM\Table(name="items") 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
*/ 
class Items 
{ 
    private $filenameForRemove; 

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

    protected function getUploadDir() 
    { 
     return 'images/items/'; 
    } 

    protected function getUploadRootDir() 
    { 
     return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
    } 

    public function getWebPath() 
    { 
     return null === $this->file ? null : $this->getUploadDir().'/'.$this->getNameEn(); 
    } 

    public function getAbsolutePath() 
    { 
     return null === $this->file ? null : $this->getUploadRootDir().'/'.$this->getNameEn().'.jpg'; 
    } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     if (null !== $this->file) 
     { 
     $this->file = $this->getId() .'.'. $this->file->guessExtension(); 
     } 
    } 

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

     $this->file->move($this->getUploadRootDir(), $this->file); 
     unset($this->file); 
    } 

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

그리고 컨트롤러 :

public function new_productAction(Request $request) 
{ 
    $product = new Items(); 
    $product->setPrice(0); 

    $form = $this->createFormBuilder($product) 
    ->add('Type', 'choice', array(
     'choices' => array('1' => 'Product', '0' => 'Article'), 
     'required' => false,)) 
    ->add('Price', 'number') 
    ->add('nameEn', 'text') 
    ->add('file', 'file', array('label' => 'Image', 'required' => true)) 
    ->getForm(); 

    if ($request->getMethod() == 'POST') 
    { 
    if ($form->isValid()) 
    { 
     $form->bindRequest($request); 
     $em = $this->getDoctrine()->getEntityManager(); 
     $em->persist($product); 
     $em->flush(); 

     return new Response('<html><body>Success!</body></html>'); 
    } 
    } 

    return $this->render('MyBundle:Default:admin_page.html.twig', array(
    'form' => $form->createView(), 
)); 
} 

심포니 버전 여기

Fatal error: Call to a member function move() on a non-object in /home/projectpath/src/BS/MyBundle/Entity/Items.php on line 327 Call Stack: 0.0002 333264 1. {main}() /home/projectpath/web/app_dev.php:0 0.0450 1158160...

엔티티 클래스의를 : 2.1.0

답변

0

php.ini 파일을 확인하고 e post_max_size와 upload_max_filesize는 모두 충분히 크게 설정됩니다.

0

나는 duke_nukem이 다른 사람이 질문을 통해 오는 경우 더 이상 6 개월 선 아래로하지만, 내가 똑같은 문제가되었다 걱정입니다 가정하고 여기에 좋은 대답을 얻었다하지 않습니다

Error with file upload in symfony 2

duke_nukem처럼 보입니다. 나는 똑같은 실수를했습니다. preUpload() 메소드 읽어야

/** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     if (null !== $this->file) 
     { 
     $this->path = $this->getId() .'.'. $this->file->guessExtension(); 
     } 
    } 

본 코드는 오류를 일으키는 $this->file 문자열로 변환한다. 경로는 실제로 $this->path에 할당되어야합니다.

다른 질문에서 Sybio가 이것을 알아 냈습니다. 나는 단지 사랑을 전하고 싶다.

0

이상한 점

코드가 컨트롤러에 잘못되었습니다. 유효성 확인을하기 전에 양식에 요청을 묶어야합니다. 그 후에 데이터를 검색 할 수 있습니다.

if ($request->getMethod() == 'POST') 
    { 
    //Note: bindRequest is now deprecated 
    $form->bind($request); 
    if ($form->isValid()) 
    { 
     //retrieve your model hydrated with your form values 
     $product = $form->getData(); 

     //has upload file ? 
     if($product->getFile() instanceof UploadedFile){ 
     //you can do your upload logic here wihtout doctrine event if you want 
     } 

     $em = $this->getDoctrine()->getEntityManager(); 
     $em->persist($product); 
     $em->flush(); 

     return new Response('<html><body>Success!</body></html>'); 
    } 
    }