You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Accessing lines by their column name ($line['first_name']) is better than accessing it with its position index ($line[0]) IMHO.
First, it is safer because if you ever make a mistake with the position you might copy the wrong data in the wrong field. That can lead to huge problems and you will need to delete all and import again no matter what. When you use the column name, worst case scenario the column is misspelled so data will be blank or you can stop execution right from the first line.
It is also much more convenient since it allows us to be able to add a new column later on the road in our CSV file and not necessarily place it at the end. We can insert the new column where it makes the most sense without having to rewrite and retest all the import code.
It also makes the code more readable, understandable, easier to maintain...
Here's what I did to pass the line as an associative array with the header key => line value:
$lines = [];
$is_headers_line = TRUE;
$headers = [];
while ($line = fgetcsv($handle, 4096, ';')) {
if ($is_headers_line) {
$headers = $line;
$is_headers_line = FALSE;
}
else {
$lines[] = $line;
}
}
$nb_lines = count($lines);
foreach ($lines as $line) {
$line_assoc = array_combine($headers, $line);
// Use base64_encode to ensure we don't overload the batch
// processor by stuffing complex objects into it.
$batch['operations'][] = [
'\Drupal\csvimport\Batch\CsvImportBatch::csvimportImportLine',
[array_map('base64_encode', $line_assoc), $csvupload, $nb_lines],
];
}
The text was updated successfully, but these errors were encountered:
If you want to propose any of these as changes for inclusion in the module by opening pull requests, I'm happy to look at merging them. Some (like this) might not suit a PR (an example module like this won't know in advance the column names the person using it needs), but might suit a change to the module's README or docs otherwise.
Hope the code has been useful to you and really appreciate you engaging with it so proactively!
Accessing lines by their column name ($line['first_name']) is better than accessing it with its position index ($line[0]) IMHO.
First, it is safer because if you ever make a mistake with the position you might copy the wrong data in the wrong field. That can lead to huge problems and you will need to delete all and import again no matter what. When you use the column name, worst case scenario the column is misspelled so data will be blank or you can stop execution right from the first line.
It is also much more convenient since it allows us to be able to add a new column later on the road in our CSV file and not necessarily place it at the end. We can insert the new column where it makes the most sense without having to rewrite and retest all the import code.
It also makes the code more readable, understandable, easier to maintain...
Here's what I did to pass the line as an associative array with the header key => line value:
The text was updated successfully, but these errors were encountered: