创建用户后,他上传文件。
我想编辑这个用户,但是,当他保存而不选择任何文件时,换句话说,他想保留原文,我得到这个错误:
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'));
}
}
}
如果我尝试上传文件也无法正常工作。同样的错误。
答案 0 :(得分:2)
首先,要排除该字段,只需在调用save之前取消设置数组中的键:
unset($this->Attorney->data['Attorney']['arquivo']);
其次,对于文件上传,您应该考虑使用插件来帮助您 - 它将为您节省一个痛苦的世界!我使用https://github.com/josegonzalez/upload
答案 1 :(得分:0)
看看:
`arquivo` = Array
原始MySQL查询不会处理数组。您需要将Array处理为MySQL将理解的数据类型。