2013-04-23 21 views
0

我有一個關於上傳的symfony2問題。我使用的是教義,有兩個實體,第一個叫做「問題」,有許多附件,第二個叫做「附件」。我正在使用一種動態數據解決方案,使人們可以在「問題」表單上上傳許多附件,併爲每個上傳標題。當我保存「問題」時,關聯的附件行也被保存,但只有標題:(上傳的實際文件永遠不會被「上傳」方法捕獲。我遵循symfony2的食譜教程,並想知道如果我錯過了一步?我有點失去了,在我的學習曲線很早就不知道如何調試這個問題Symfony2 - 如何通過表格上傳hasMany附件

我的連接實體內容如下:

namespace WebConfection\ServiceDeskBundle\Entity; 

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

/** 
* @ORM\Table(name="webconfection_servicedesk_attachment") 
* @ORM\Entity(repositoryClass="WebConfection\ServiceDeskBundle\Repository\AttachmentRepository") 
* @ORM\HasLifecycleCallbacks() 
*/ 
class Attachment 
{ 

    private $temp; 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=150) 
    */ 
    protected $title;  

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

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

    /** 
    * @ORM\Column(type="datetime") 
    */ 
    protected $created; 

    /** 
    * @ORM\Column(type="datetime") 
    */ 
    protected $updated; 

    /** 
    * @ORM\ManyToOne(targetEntity="Issue", inversedBy="attachments") 
    * @ORM\JoinColumn(name="issue_id", referencedColumnName="id") 
    */ 
    protected $issue; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 

     $this->setCreated(new \DateTime()); 
     $this->setUpdated(new \DateTime()); 
    }  

    /** 
    * Sets file. 
    * 
    * @param UploadedFile $file 
    */ 
    public function setFile(UploadedFile $file = null) 
    { 
     $this->file = $file; 
     // check if we have an old image path 
     if (is_file($this->getAbsolutePath())) { 
      // store the old name to delete after the update 
      $this->temp = $this->getAbsolutePath(); 
     } else { 
      $this->path = 'initial'; 
     } 
    } 

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


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

     // check if we have an old image 
     if (isset($this->temp)) { 
      // delete the old image 
      unlink($this->temp); 
      // clear the temp image path 
      $this->temp = null; 
     } 

     // you must throw an exception here if the file cannot be moved 
     // so that the entity is not persisted to the database 
     // which the UploadedFile move() method does 
     $this->getFile()->move(
      $this->getUploadRootDir(), 
      $this->id.'.'.$this->getFile()->guessExtension() 
     ); 

     $this->setFile(null); 
    } 

    /** 
    * @ORM\PreRemove() 
    */ 
    public function storeFilenameForRemove() 
    { 
     $this->temp = $this->getAbsolutePath(); 
    } 

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

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

    /** 
    * Get file. 
    * 
    * @return UploadedFile 
    */ 
    public function getFile() 
    { 
     return $this->file; 
    } 

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

    /** 
    * Set title 
    * 
    * @param string $title 
    * @return Attachment 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

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

    /** 
    * Set created 
    * 
    * @param \DateTime $created 
    * @return Attachment 
    */ 
    public function setCreated($created) 
    { 
     $this->created = $created; 

     return $this; 
    } 

    /** 
    * Get created 
    * 
    * @return \DateTime 
    */ 
    public function getCreated() 
    { 
     return $this->created; 
    } 

    /** 
    * Set updated 
    * 
    * @param \DateTime $updated 
    * @return Attachment 
    */ 
    public function setUpdated($updated) 
    { 
     $this->updated = $updated; 

     return $this; 
    } 

    /** 
    * Get updated 
    * 
    * @return \DateTime 
    */ 
    public function getUpdated() 
    { 
     return $this->updated; 
    } 
} 

回答

2

我有一個類似的問題,這裏是如何我解決了它

http://blog.icod.de/2013/04/17/symfony2-file-upload-with-related-entities/

不介意我的演說,並在Symfony2中肆虐,我需要在一些地方偶爾發泄;)

它可能不是一個具體的回答你的問題,但也許它會幫助你

我做在控制器中移動,我當然可以將這個功能移動到實體上,但是,它可以工作,我只需要上傳一次功能,這會減少內存使用量,而不是在實體中。

HTH