使用CakePHP将CSV导入数据库

时间:2014-12-05 08:45:19

标签: php cakephp csv

我有一个小问题。我已经尝试了很多不同的方法,但我找不到合适的解决方案。也许伙计们,你能帮助我吗?如果我尝试导入和数组,我会遇到一些错误。

数据库名称:kontaktid

表格名称:kontaktids

以下是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导入数据库,但我没有找到任何有效的解决方案。有人可以给我提供线索或解决方法,如何正确行事。

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

1 个答案:

答案 0 :(得分:0)

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

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

同样适用于$error它不存在,因此无法进行评估。您需要先定义它们并为它们分配值,然后才能执行任何操作。