@@ -27,14 +27,15 @@ class CsvSeeder extends Seeder
2727 protected $ filename ;
2828
2929 /**
30- * DB field that to be hashed, most likely a password field.
31- * If your password has a different name, please overload this
30+ * DB field that to be hashed, most likely a password field.
31+ * If your password has a different name, please overload this
3232 * variable from our seeder class.
33- *
33+ *
3434 * @var string
3535 */
36-
36+
3737 protected $ hashable = 'password ' ;
38+
3839 /**
3940 * An SQL INSERT query will execute every time this number of rows
4041 * are read from the CSV. Without this, large INSERTS will silently
@@ -84,7 +85,7 @@ private function strip_utf8_bom( $text )
8485 */
8586 private function seedFromCSV ($ filename , $ deliminator = ", " )
8687 {
87- if ( !file_exists ($ filename ) || !is_readable ($ filename ) )
88+ if ( !file_exists ($ filename ) || !is_readable ($ filename ) )
8889 return FALSE ;
8990
9091 $ header = NULL ;
@@ -94,46 +95,46 @@ private function seedFromCSV($filename, $deliminator = ",")
9495 if ( ($ handle = fopen ($ filename , 'r ' )) !== FALSE )
9596 {
9697 while ( ($ row = fgetcsv ($ handle , 0 , $ deliminator )) !== FALSE )
97- {
98+ {
9899
99100 if ( !$ header )
100101 {
101102 $ header = $ row ;
102103 $ header [0 ] = $ this ->strip_utf8_bom ($ header [0 ]);
103104 }
104105 else
105- {
106- // insert only non-empty fields from the csv file
107- $ i = 0 ;
108- $ row_values = [];
109-
110- foreach ($ header as $ key ) {
111- if (!empty ($ row [$ i ])) {
112- $ row_values [$ key ] = $ row [$ i ];
113- }
114- $ i ++;
115- }
116-
117- if (isset ($ row_values [$ this ->hashable ])){
118- $ row_values [$ this ->hashable ] = Hash::make ($ row_values [$ this ->hashable ]);
119- }
120-
121- $ data [$ row_count ] = $ row_values ;
122-
123- // Chunk size reached, insert
124- if ( ++$ row_count == $ this ->insert_chunk_size )
125- {
126- $ this ->run_insert ($ data );
127- $ row_count = 0 ;
128- //clear the data array explicitly when it was inserted so that nothing is left, otherwise a leftover scenario can cause duplicate inserts
129- $ data = array ();
130- }
106+ {
107+ // insert only non-empty fields from the csv file
108+ $ i = 0 ;
109+ $ row_values = [];
110+
111+ foreach ($ header as $ key ) {
112+ if (!empty ($ row [$ i ])) {
113+ $ row_values [$ key ] = $ row [$ i ];
114+ }
115+ $ i ++;
116+ }
117+
118+ if (isset ($ row_values [$ this ->hashable ])){
119+ $ row_values [$ this ->hashable ] = Hash::make ($ row_values [$ this ->hashable ]);
120+ }
121+
122+ $ data [$ row_count ] = $ row_values ;
123+
124+ // Chunk size reached, insert
125+ if ( ++$ row_count == $ this ->insert_chunk_size )
126+ {
127+ $ this ->run_insert ($ data );
128+ $ row_count = 0 ;
129+ //clear the data array explicitly when it was inserted so that nothing is left, otherwise a leftover scenario can cause duplicate inserts
130+ $ data = array ();
131+ }
131132 }
132133 }
133134
134135 // Insert any leftover rows
135136 //check if the data array explicitly if there are any values left to be inserted, if insert them
136- if ( count ($ data ) > 0 )
137+ if ( count ($ data ) )
137138 $ this ->run_insert ($ data );
138139
139140 fclose ($ handle );
0 commit comments