使用cakephp将数据从csv导入到mysql [英] import data from csv to mysql using cakephp

查看:183
本文介绍了使用cakephp将数据从csv导入到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用cakephp从csv导入数据到mysql表。

当我运行该函数时,它将null值放在我的表中,我相信它是我的$ data数组的格式,这里是我的代码。

  var $ name ='ScWidths'; 
var $ scaffold;

/ *
function import(){
$ messages = $ this-> ScWidth-> import('scwidths.csv');
$ this-> set('messages',$ messages);
}

* /

  function import(){
//为了避免调整
// $ data的内容,你应该使用你的db字段名作为标题名
//例如:Post.id,Post.title,Post.description


//设置从
$ filename = TMP读取CSV的文件名。 'uploads'。 DS。 'Sc_widths'。 DS。 'scwidths.csv';


//打开文件
$ handle = fopen($ filename,r);


//读取第一行作为标题
$ header = fgetcsv($ handle);


//创建消息容器
$ return = array(
'messages'=> array(),
'errors'=> ; array(),
);


//读取文件中的每个数据行
$ i = 0;
while(($ row = fgetcsv($ handle))!== FALSE){
$ i ++;
$ data = array();


//每个头字段
foreach($ header as $ k => $ head){

//获取数据字段from Model.field
if(strpos($ head,'。')!== false){

$ h = explode('。',$ head);
#die(debug($ h));
$ data [$ h [0]] [$ h [1]] = isset($ row [$ k])? $ row [$ k]:'';

}

//从字段$ b获取数据字段$ b else {
$ data ['ScWidth'] [$ head] = isset row [$ k])? $ row [$ k]:'';
}

}



$ data ['ScWidth'] ['section_id'] = 1;
$ this-> ScWidth-> create();





//成功或不成功:/
if($ this-> ScWidth-> save )){
echosuccess;

}
}


//关闭文件
fclose($ handle);


//返回消息
// return $ return;


}

  array(
'ScWidths'=> array(
'chainage'=>'0 '

),
'Left_side'=>'3.7'

),
'right_side'=> '3.7'

),
'section_id'=> (int)1


p>

没有...



我相信我的下一个错误是在我的保存方法。



这里是我的数据文件,只是incase。

  ScWidths。 chainage,ScWidths.Left_side,ScWidths.right_side 
0,3.7,3.7
10,3.7,3.7
20,3.7,3.7
30,3.7,3.7
40 ,3.7,3.7
50,3.7,3.7
60,3.7,3.7
70,3.7,3.7
80,3.7,3.7



更正了我的代码,如果任何人需要一个示例,这是工作100%)

解决方案

此处的行不正确



$ data [$ h [0]] [$ h [1]] [$ h [2]] =(isset($ row [$ k]))? $ row [$ k]:'';



$ h [2] 将永远不会被定义。



当您爆炸一个标题名称,例如sc_widths.chainage(用'。'作为分隔符)时,您会得到




  • $ h [0] ='sc_widths';

  • $ h [1] ='chainage';





因此,您的特定问题的修复是删除$ h [2]:



$ data [$ h [0]] [$ h [1]] =(isset($ row [$ k]))? $ row [$ k]:'';


I want to import data from csv to mysql table using cakephp.
When I run the function it places null values in my table and I believe it is the format of my $data array, here is my code.

var $name = 'ScWidths';
    var $scaffold;

/*
    function import() {
            $messages = $this->ScWidth->import('scwidths.csv');
            $this->set('messages', $messages);
    }

*/

 function import() {
             // 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 . 'Sc_widths' . DS . 'scwidths.csv';


            // 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
            $i = 0;
             while (($row = fgetcsv($handle)) !== FALSE) {
                     $i++;
                     $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);
                 #die(debug($h));
                                     $data[$h[0]][$h[1]]=isset($row[$k]) ? $row[$k] : '';

                            }

                             // get the data field from field
                            else {
                                     $data['ScWidth'][$head]=isset($row[$k]) ? $row[$k]: '';
                            }

                     }



    $data['ScWidth']['section_id']=1;
    $this->ScWidth->create();





                     // success or not :/
                    if ($this->ScWidth->save($data)) {  
            echo "success";

                    }
             }


             // close the file
             fclose($handle);


             // return the messages
             //return $return;


    }

at my debug point it returns this array

array(
    'ScWidths' => array(
        'chainage' => '0'

        ),
        'Left_side' => '3.7'

        ),
        'right_side' => '3.7'

        ),
        'section_id' => (int) 1
    )
)

error log im getting,

nothing...

I believe my next error is in my save method.

here is my data file, just incase.

ScWidths.chainage,ScWidths.Left_side,ScWidths.right_side
0,3.7,3.7
10,3.7,3.7
20,3.7,3.7
30,3.7,3.7
40,3.7,3.7
50,3.7,3.7
60,3.7,3.7
70,3.7,3.7
80,3.7,3.7

corrected my code, this is working 100% if anyone needs an example :)

解决方案

This line here is in-correct

$data[$h[0]][$h[1]][$h[2]]=(isset($row[$k])) ? $row[$k] : '';

$h[2] will never be defined.

When you explode a header name like "sc_widths.chainage" (with '.' as separator) you will get

  • $h[0] = 'sc_widths';
  • $h[1] = 'chainage';

You will never get a $h[2] with that data.

So the fix for your particular problem is to drop the $h[2] like this:

$data[$h[0]][$h[1]]=(isset($row[$k])) ? $row[$k] : '';

这篇关于使用cakephp将数据从csv导入到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆