编辑文件上传操作



在本教程中,我能够很好地创建文件上传,并且根据本教程,editAction与Add操作几乎相同。

如果我没有在editAction中添加文件上传代码,它可以正常工作,但在添加文件上传后,在exchangearray()中显示错误

无法在第20行modulereportsrcreportModelCompanyReport.php中使用ModelCompanyReport类型的对象作为数组

模型文件

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 ZendFileTransferAdapterHttp();
            //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

我认为这不是文件上传代码的问题。您已经将实体绑定到表单:

$form->bind($companyreport);

$form->setData($data)被执行时,表单将提交的数据与实体绑定。这意味着$form->getData()将返回包含所有这些数据的实体。这就是错误所说的,它返回一个实体(CompanyReport),而不是一个数组。

$form->getData() (ZendFormFormgetData())执行的代码为:

    //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作业'是由表单本身完成的。

最新更新