使用 CakePHP 将 CSV 导入数据库


Import CSV into database using CakePHP

我有一个问题。我已经尝试了许多不同的方法,但没有找到正确的解决方案。也许伙计们,你可以帮我?如果我尝试导入和数组,我遇到了一些错误。

数据库名称:kontaktid

贝尔名称:康塔克蒂兹

这是我在KontaktidController.php中的函数:

function import() {
        $messages = $this->Kontaktid->import('export.csv');
        debug($messages);
}

这是我的模型文件Kontaktid.php

<?php   

class Kontaktid extends AppModel {
    public $validate = array(
        'title' => array(
            'rule' => 'notEmpty'
        ),
        'body' => array(
            'rule' => 'notEmpty'
        )
    );
    var $name = 'Kontaktid';
    function import($filename) {
        // to avoid having to tweak the contents of
        // $data you should use your db field name as the heading name
        // eg: Post.id, Post.title, Post.description
        // set the filename to read CSV from
        $filename = TMP . 'uploads' . DS . 'Kontaktid' . DS . $filename;
        // open the file
        $handle = fopen($filename, "r");
        // read the 1st row as headings
        $header = fgetcsv($handle);
        // create a message container
        $return = array(
            'messages' => array(),
            'errors' => array(),
        );
        // read each data row in the file
        while (($row = fgetcsv($handle)) !== FALSE) {
            $i++;   /*This is line 38*/
            $data = array();
            // for each header field
            foreach ($header as $k=>$head) {
                // get the data field from Model.field
                if (strpos($head,'.')!==false) {
                    $h = explode('.',$head);
                    $data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';
                }
                // get the data field from field
                else {
                    $data['kontaktid'][$head]=(isset($row[$k])) ? $row[$k] : '';
                }
            }
            // see if we have an id            
            $id = isset($data['kontaktid']['id']) ? $data['kontaktid']['id'] : 0;
            // we have an id, so we update
            if ($id) {
                // there is 2 options here,
                // option 1:
                // load the current row, and merge it with the new data
                //$this->recursive = -1;
                //$post = $this->read(null,$id);
                //$data['Post'] = array_merge($post['Post'],$data['Post']);
                // option 2:
                // set the model id
                $this->id = $id;
            }
            // or create a new record
            else {
                $this->create();
            }
            // see what we have
            // debug($data);
            // validate the row
            $this->set($data);
            if (!$this->validates()) {
                $this->_flash('warning');
                $return['errors'][] = __(sprintf('Post for Row %d failed to validate.',$i), true);
            }
            // save the row
            if (!$error && !$this->save($data)) {    /*This is line 88*/
                $return['errors'][] = __(sprintf('Post for Row %d failed to save.',$i), true);
            }
            // success message!
            if (!$error) {     /*This is line 93*/
                $return['messages'][] = __(sprintf('Post for Row %d was saved.',$i), true);
            }
        }
        // close the file
        fclose($handle);
        // return the messages
        return $return;
    }
}

这是我得到的错误:

Notice (8): Undefined variable: i [APP'Model'Kontaktid.php, line 38]
Notice (8): Undefined variable: error [APP'Model'Kontaktid.php, line 88]
Notice (8): Undefined variable: error [APP'Model'Kontaktid.php, line 93]

这是数组:

array(
    'messages' => array(
        (int) 0 => 'Post for Row 1 was saved.',
        (int) 1 => 'Post for Row 2 was saved.',
        (int) 2 => 'Post for Row 3 was saved.',
        (int) 3 => 'Post for Row 4 was saved.',
        (int) 4 => 'Post for Row 5 was saved.',
        (int) 5 => 'Post for Row 6 was saved.',
        (int) 6 => 'Post for Row 7 was saved.',
        (int) 7 => 'Post for Row 8 was saved.',
        (int) 8 => 'Post for Row 9 was saved.',
        (int) 9 => 'Post for Row 10 was saved.',
        (int) 10 => 'Post for Row 11 was saved.'
    ),
    'errors' => array(
        (int) 0 => 'Post for Row 1 failed to save.',
        (int) 1 => 'Post for Row 2 failed to save.',
        (int) 2 => 'Post for Row 3 failed to save.',
        (int) 3 => 'Post for Row 4 failed to save.',
        (int) 4 => 'Post for Row 5 failed to save.',
        (int) 5 => 'Post for Row 6 failed to save.',
        (int) 6 => 'Post for Row 7 failed to save.',
        (int) 7 => 'Post for Row 8 failed to save.',
        (int) 8 => 'Post for Row 9 failed to save.',
        (int) 9 => 'Post for Row 10 failed to save.',
        (int) 10 => 'Post for Row 11 failed to save.'
    )
)

我尝试了许多不同的方法如何将csv导入数据库,但是我没有找到任何有效的解决方案。有人可以给我线索或一些解决方案,如何做对。

感谢您提供任何线索或解决方案。

你还没有定义变量,这就是它抛出错误的原因。

$i不是在 while 循环之外定义的,并且不可用于评估或增量。

$error也是如此,

它不存在,因此无法评估。您需要先定义它们并为它们赋值,然后才能执行任何操作。