首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组存储cakephp 3 savemany

数组存储cakephp 3 savemany
EN

Stack Overflow用户
提问于 2017-09-14 18:04:54
回答 1查看 371关注 0票数 0

嗨,有人知道这一点吗?我是Cakephp的初学者,我试着上传多张图片,但不能保存。

控制器:

代码语言:javascript
复制
public function add() {
if ($this->request->is('post')) {
    //$data = $this->request->getData();
    if(!empty($_FILES['photo']['name'])){
        $count = count($_FILES['photo']['name']);
        for ($i=0; $i < $count; $i++) { 
            $filename = $_FILES['photo']['name'][$i];
            $type = $_FILES['photo']['type'][$i];
            $tmp = $_FILES['photo']['tmp_name'][$i];
            $error = $_FILES['photo']['error'][$i];
            $size = $_FILES['photo']['size'][$i];
            $uploadPath = '../uploads/files/';

            $file[$i]['user_id'] = $this->Auth->user('id');
            $file[$i]['filename'] = $filename;
            $file[$i]['file_location'] = $uploadPath;
            $file[$i]['file_type'] = $type;
            $file[$i]['file_size'] = $size;
            $file[$i]['file_status'] = 'Active';
            $file[$i]['created'] = date("Y-m-d H:i:s");
            $file[$i]['modified'] = date("Y-m-d H:i:s");
        }
        $table = TableRegistry::get('files');
        $entities = $table->newEntities($file);
        if($table->saveMany($entities)) {
            $this->Flash->success(__('File has been uploaded and inserted successfully.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('Unable to upload file, please try again.'));
        }

    } else {
        $this->Flash->error(__('Please choose a file to upload.'));
    }
}

}但是当我试着调试所有的东西,但是在保存的时候它就不能工作了!我的代码有问题吗,有人能帮我修复我的add函数吗?

查看:

代码语言:javascript
复制
echo $this->Form->input('photo[]', ['type' => 'file','multiple' => 'true','label' => 'Upload Multiple Photos']);
EN

回答 1

Stack Overflow用户

发布于 2017-09-16 03:28:31

您正在尝试使用saveMany()保存一条记录。

代码语言:javascript
复制
public function add()
{
    if ($this->request->is('post')) {
        $table = TableRegistry::get('files');
        $uploadPath = '../uploads/files/';

        if(!empty($_FILES['photo'])){
            foreach ($_FILES['photo'] as $EachPhoto) {
                $data[] = [
                    'user_id' => $this->Auth->user('id'),
                    'filename' => $EachPhoto['name'],
                    'file_location' => $uploadPath,
                    'file_type' => $EachPhoto['type'],
                    'file_size' => $EachPhoto['size'],
                    'file_status' => 'Active',
                    'created' => date("Y-m-d H:i:s")
                ];
            }

            $entities = $table->newEntities($data);
            if($this->Files->saveMany($entitie)) {
                    $this->Flash->success(__('File has been uploaded and inserted successfully.'));
                    return $this->redirect(['action' => 'index']);
            } else {
                    $this->Flash->error(__('Unable to upload file, please try again.'));
            }

        } else {
            $this->Flash->error(__('Please choose a file to upload.'));
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46216197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档