2013-05-20 37 views
0

当用户创建时,他上传文件。

我想修改这个用户,但是,当他保存和未选择任何文件,换句话说,他想保持原来的,我得到这个错误:

SQL Query: UPDATE `societario`.`attorneys` SET `nome` = 'teste', `empresa` = 'Sotreq', `filial` = 'Matriz', `unidade` = 'Energia', `alcada` = 'Até 50.000', `validade` = '', `arquivo` = Array WHERE `societario`.`attorneys`.`id` = '42' 

如果用户不选择任何文件,我想,保存不得到的$this->Attorney->data['Attorney']['arquivo']

我edit.php

function edit($id = null) { 



    $this->Attorney->id = $id; 
    $this->set('poderes',$this->Attorney->Power->find('list', array('fields' => 'resumo'))); 

     if ($this->request->is('get')) { 
      $this->request->data = $this->Attorney->read(); 
     } else { 

      if ($this->Attorney->save($this->request->data)) { 

      $targetFolder = 'societario/app/webroot/uploads/'; // Relative to the root 
      $tempFile = $this->request->data['Attorney']['arquivo']['tmp_name']; 
      $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder; 
      $targetFile = rtrim($targetPath,'/') . '/' . $this->request->data['Attorney']['arquivo']['name'];; 
      move_uploaded_file($tempFile,$targetFile); 
      $this->Attorney->updateAll(
      array('arquivo' => "'".$this->request->data['Attorney']['arquivo']['name'] ."'"), 
      array('id' => $id));  

      $this->Session->setFlash('Usuário editado com sucesso!', 'default', array('class' => 'flash_sucess')); 
      $this->redirect(array('action' => 'usuarios')); 
    } 
} 

} 

如果我尝试上传文件不能正常工作或者。同样的错误。

回答

2

首先,以排除现场,正好被清除数组中键,你可以调用保存之前:

unset($this->Attorney->data['Attorney']['arquivo']); 

二,文件上传,你应该考虑使用一个插件来帮助你 - 它会为你节省一个痛苦的世界!我使用https://github.com/josegonzalez/upload

0

看:

`arquivo` = Array 

原始的MySQL查询不会处理数组。您需要将数组处理为MySQL将理解的数据类型。

+0

是的,我知道......如果没有选择文件,我想排除该字段。 –

+1

啊,好的。 @ joshua.paling已经得到你需要的东西。只需要在没有文件上传的情况下将其置入条件。 'if(!isset($ this-> request-> data ['Attorney'] ['arquivo'] ['tmp_name'])){...' – Gor