2013-04-08 42 views
0

tutorial之後,我們就可以很好地創建文件上傳了。根據zend教程,editAction與Add操作幾乎相同。文件上傳的編輯操作

如果我不添加文件上傳代碼editAction它工作正常,但添加文件上傳後,顯示exchangearray)錯誤(

不能使用類型的對象\型號\ CompanyReport比如\模塊\報告陣列\第20行

這裏的src \報告\型號\ CompanyReport.php是模型文件

public function exchangeArray($data) 
{ 
    <-this is line number 20->$this->id  = (isset($data['id'])) ? $data['id'] : null; 
    $this->title = (isset($data['title'])) ? $data['title'] : null; 
    $this->company = (isset($data['company'])) ? $data['company'] : null; 
    $this->sector = (isset($data['sector'])) ? $data['sector'] : null; 
    $this->report_date = (isset($data['report_date'])) ? $data['report_date'] : null; 
    $this->report_file = (isset($data['report_file'])) ? $data['report_file'] : null; 

} 

以下是editAction代碼

public function editAction(){ 
    $id = (int) $this->params()->fromRoute('id', 0); 
    if (!$id) { 
     return $this->redirect()->toRoute('companyreport', array(
      'action' => 'add' 
     )); 
    } 

    // Get the Album with the specified id. An exception is thrown 
    // if it cannot be found, in which case go to the index page. 
    try { 
     $companyreport = $this->getCompanyReportTable()->getCompanyReport($id); 
    } 
    catch (\Exception $ex) { 
     return $this->redirect()->toRoute('companyreport', array(
      'action' => 'index' 
     )); 
    } 

    $form = new CompanyReportForm(); 
    $form->bind($companyreport); 
    $form->get('submit')->setAttribute('value', 'Edit'); 

    $request = $this->getRequest(); 
    if ($request->isPost()) { 

     $companyreport = new CompanyReport(); 
     $form->setInputFilter($companyreport->getInputFilter()); 

     $nonFile = $request->getPost()->toArray(); 
     $File = $this->params()->fromFiles('report_file'); 

     $data = array_merge_recursive(
        $this->getRequest()->getPost()->toArray(),   
        $this->getRequest()->getFiles()->toArray() 
       ); 
     $form->setData($data); 

     if ($form->isValid()) { 

      $size = new Size(array('min'=>500000)); //minimum bytes filesize 

      $adapter = new \Zend\File\Transfer\Adapter\Http(); 
      //validator can be more than one... 
      $adapter->setValidators(array($size), $File['name']); 

      if (!$adapter->isValid()){ 
       $dataError = $adapter->getMessages(); 
       $error = array(); 
       foreach($dataError as $key=>$row) 
       { 
        $error[] = $row; 
       } //set formElementErrors 
       $form->setMessages(array('report_file'=>$error)); 
      } else { 

       $adapter->setDestination(dirname(__DIR__).'/company_reports'); 
       if ($adapter->receive($File['name'])) { 
        $companyreport->exchangeArray($form->getData()); 
        $this->getCompanyReportTable()->saveCompanyReport($companyreport); 

       // Redirect to list of albums 
       return $this->redirect()->toRoute('companyreport'); 
       } 
      } 
     } 
    } 

    return array(
     'id' => $id, 
     'form' => $form, 
    ); 
} 

請建議editAction進行文件上傳。

回答

0

我認爲這不是文件上傳代碼的問題。你已經綁定的實體形式:

$form->bind($companyreport); 

當執行$form->setData($data),形式與實體結合的發佈數據。這意味着$form->getData()將返回填充了所有數據的實體。這就是錯誤所說的,它返回一個實體(CompanyReport),而不是數組。

$form->getData()Zend\Form\Form\getData())執行的代碼是:

//IF YOU BIND, $this->object WILL BE THE ENTITY, SO IT'LL RETURN IT  
    if (($flag !== FormInterface::VALUES_AS_ARRAY) && is_object($this->object)) { 
     return $this->object; 
    } 

    //OTHERWISE IT'LL RETURN AN ARRAY WITH POSTED DATA 
    $filter = $this->getInputFilter(); 

    if ($flag === FormInterface::VALUES_RAW) { 
     return $filter->getRawValues(); 
    } 

    return $filter->getValues(); 

如果你想用你的exchangeArray功能,你可以通過它的合併陣列$data。但是沒有必要,當你將表單綁定到實體時,'exchangeArray作業'是由表單本身完成的。