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'));
    }
}

}

如果我尝试上传文件也不起作用。同样的错误。

4

2 回答 2

2

首先,要排除该字段,只需在调用 save 之前取消设置数组中的该键:

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

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

于 2013-05-20T02:36:52.657 回答
0

看着:

`arquivo` = Array

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

于 2013-05-20T00:12:49.850 回答