且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用cakephp将数据从csv导入到mysql

更新时间:2022-10-17 23:15:31

此处的行不正确



$ 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] : '';