2013-05-27 29 views
-1

我按照與Symfony2的多文件上傳,並創建了一個實體沒有能夠得到上傳文件的文件名中的Symfony2

/* 
* @ORM\HasLifecycleCallbacks 
* @ORM\Entity(repositoryClass="Repair\StoreBundle\Entity\attachmentsRepository") 
*/ 

class attachments 
{ 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 

    private $id; 
    /** 
    * @var string 
    * 
    * @Assert\File(maxSize="6000000") 
    * @ORM\Column(name="files", type="array", length=255, nullable=true) 
    */ 
    private $files=array(); 

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

    /** 
    * Set files 
    * @param object $files 
    * 
    * @return attachments 
    */ 
    public function setFiles($files) { 
     $this->files = $files; 
    } 

    /** 
    * Get files 
    * 
    * @return object 
    */ 
    public function getFiles() { 
     return $this->files; 
    } 

    public function __construct() { 
     $files = array(); 
    } 

    public function uploadFiles() { 
     // the files property can be empty if the field is not required 
     if (null === $this->files) { 
      return; 
     } 

     if (!$this->id) { 
       $this->files->move($this->getTmpUploadRootDir(), $this->files->getClientOriginalName()); 

     } else { 
      $this->files->move($this->getUploadRootDir(), $this->files->getClientOriginalName()); 
     } 

     $this->setFiles($this->files->getClientOriginalName()); 
    } 

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

    public function getWebPath() { 
     return null === $this->path 
      ? null 
      : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path; 
    } 

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

    protected function getUploadDir() { 
     return 'uploads/'; 
    } 

} 

我有具有如下因素代碼

class uploadController extends Controller 
{ 

    public function uploadAction(Request $request) { 

     $id= $_GET['id']; 

     $user = new attachments(); 

     $form = $this->createFormBuilder($user)->add('files','file',array("attr"=>array("multiple" =>"multiple",)))->getForm(); 

     $formView = $form->createView(); 

     $formView->getChild('files')->set('full_name','form[file][]'); 

     if ($request->getMethod() == 'POST') { 

      $em = $this->getDoctrine()->getManager(); 
      $form->bind($request); 
      $files = $form["files"]->getFilenames(); 
      $user->uploadFiles(); //--here iam not able to get te file names in order to send to the upload function.upload files is returning null values 

     } 
    } 
} 

的控制器控制器無法獲取uder從視圖中上傳的文件名。當我發送到實體中的上傳功能時,返回空值

+0

目前還不清楚你的意思,你能提供更多的信息,除了代碼? –

+0

我想上傳多個文件。爲此,我需要獲取用戶從視圖中上傳到控制器的文件名,當我嘗試使用窗體[「文件」] - > getData檢索控制器上傳的文件時();當我傳遞$ em = $ this-> getDoctrine() - > getManager()時,它返回空值 – bhari

+0

;在上傳函數中它返回空值,即它不能從控制器獲取文件名到實體上傳。在控制器本身中,它不能從視圖中獲得文件名 – bhari

回答

0

我認爲您應該閱讀0123再次。您正確使用註釋@ORM\HasLifecycleCallbacks,但您的代碼缺少方法的正確註釋。驗證請求並保持對象後,您可以使用@ORM\PostPersist()@ORM\PostUpdate()註釋自動調用upload()函數。另外我會建議爲每個文件使用一個實體並創建一個OneToMany關係。這將使它更容易和合乎邏輯地正確地存儲你的文件。

相關問題