I am looking to extend Silverstripe's CSVBulkLoader class to do some business logic before/upon import.
In the WineAdmin Class (extending ModelAdmin), I have a custom loader defined with $model_importers property:
//WineAdmin.php
private static $model_importers = [
'Wine' => 'WineCsvBulkLoader'
];
In the WineCsvBulkLoader Class, the $columnMap property maps CSV columns to SS DataObject columns:
//WineCsvBulkLoader.php
use SilverStripe\Dev\CsvBulkLoader;
class WineCsvBulkLoader extends CsvBulkLoader
{
public $columnMap = [
// csv columns // SS DO columns
'Item Number' => 'ItemNumber',
'COUNTRY' => 'Country',
'Producer' => 'Producer',
'BrandName' => 'BrandName',
// etc
];
- When the import is run, the WineCsvBulkLoader class is being invoked, but, the column mappings do not seem to work properly, returning values only where the [key] and [value] in the array are identical. Otherwise, the imported columns are empty. What could be causing this?
Additionally, the $duplicateChecks property is set to look for duplicates.
public $duplicateChecks = [
'ItemNumber' => 'ItemNumber'
];
}
- What does the $duplicateChecks property actually do when there is a duplicate? Does it skip the record?
- Can I use callbacks here?
In the docs, I found some code for an example method that splits data in a column into 2 parts and maps those parts to separate columns on the class:
public static function importFirstAndLastName(&$obj, $val, $record)
{
$parts = explode(' ', $val);
if(count($parts) != 2) return false;
$obj->FirstName = $parts[0];
$obj->LastName = $parts[1];
}
- Is $obj the final import object? How does it get processed?
- $val seems to be the value of the column in the csv being imported. Is that correct?
- What is contained in $record?
Here are some additional enhancements I hope to make:
- Read the Byte Order Marker, if present, upon import, and do something useful with it
- Upon import, check for duplicate records, and if there are duplicates, I’d like to only update the columns in the record that have changed.
- Delete records that are already in the database, that are not in the CSV being imported
- Add whatever security measures are necessary to use this custom class securely.
- Export CSV with BOM (byte order mark as UTF8)
I'm not looking for a complete answer, but appreciative of any insights.