简体   繁体   English

如何在CAKEPHP中上传批量记录

[英]How to upload bulk records in CAKEPHP

A 4 column dashboard with 10 rows is 具有10行的4列仪表板是

provided wherein the user can enter 提供了用户可以输入的位置

details as follows: 详细信息如下:

Column 1 – File Category
Column 2 – File Type
Column 3 - File Reference
Column 4 – Upload FIle (any type)

Upload button– uploads the details of all the documents entered simultaneously 上载按钮–上载同时输入的所有文档的详细信息

I am unable to upload all records in database only last recording is saving in DATABASE kindly help me 我无法上载数据库中的所有记录,只有最后一个记录保存在数据库中,请帮我

UserControlle.php (my controller) UserControlle.php(我的控制器)

<?php

App::uses('AppController', 'Controller');

class FilesController extends AppController
{
    public $helpers=array('Html','Form');

    public $components = array('RequestHandler');   

    /*function to display all files details*/
    public function index() 
    {
        $this->set('Files', $this->File->find('all'));
    }

    /*function to add file record into the database */
    public function add() 
    {
        if ($this->request->is('post')) 
        {
            $this->File->create();
            if(empty($this->data['File']['url']['name']))
            {
                unset($this->request->data['url']['file']);
            }
            if(!empty($this->data['File']['url']['name']))
            {
                $file=$this->data['File']['url'];
                $file['name']=$this->sanitize($file['name']);
                $this->request->data['File']['url'] = time().$file['name'];

                if($this->File->save($this->request->data))
                {
                    move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']); 
                    return $this->redirect(array('action' => 'index')); 
                    $this->Session->setFlash(__('Your File has been saved.'));
                    //return $this->redirect(array('action' => 'index'));
                }
            }
            $this->Session->setFlash(__('Unable to add your File.'));
        }
    }

} 

add.ctp (my view) add.ctp(我的看法)

<h1>Add File</h1>
<?php
echo $this->Form->create('File',array('enctype'=>'multipart/form-data'));

echo $this->Form->input('category', array(
            'options' => array( 'passport' => 'Passport', 'pancard' => 'Pancard', 'driving license' => 'Driving License', 'others' => 'Others')
        ));

        echo $this->Form->input('type', array(
            'options' => array( 'image' => 'Image', 'office' => 'Office', 'technical' => 'Technical', 'others' => 'Others')
        ));

echo $this->Form->input('reference');

echo $this->Form->input('url', array('type' => 'file'));

echo $this->Form->input('category', array(
            'options' => array( 'passport' => 'Passport', 'pancard' => 'Pancard', 'driving license' => 'Driving License', 'others' => 'Others')
        ));

        echo $this->Form->input('type', array(
            'options' => array( 'image' => 'Image', 'office' => 'Office', 'technical' => 'Technical', 'others' => 'Others')
        ));

echo $this->Form->input('reference');

echo $this->Form->input('url', array('type' => 'file'));

echo $this->Form->input('category', array(
            'options' => array( 'passport' => 'Passport', 'pancard' => 'Pancard', 'driving license' => 'Driving License', 'others' => 'Others')
        ));

        echo $this->Form->input('type', array(
            'options' => array( 'image' => 'Image', 'office' => 'Office', 'technical' => 'Technical', 'others' => 'Others')
        ));

echo $this->Form->input('reference');

echo $this->Form->input('url', array('type' => 'file'));

echo $this->Form->end('Save File');

How would this bulk save anything? 这批量如何节省任何东西? It looks like you are saving the uploaded file to the db. 看来您要将上传的文件保存到数据库。 Also it is not a good idea to name a model File as it conflicts with a core class that can cause a bunch of problems. 命名模型File也不是一个好主意,因为它与可能导致大量问题的核心类冲突。

Generally if you want to bulk insert, you would want to read the rows out of the CSV, loop over them and call create() and save() on each one. 通常,如果要批量插入,则需要从CSV中读取行,将它们循环并在每行上调用create()save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM