Silverstripe Version: 3.6.6
As external data is stored like LinkName (LinkUrl), LInkName (LinkUrl), LInkName (LinkUrl), etc. in one field there is no easy column mapping possible. How do I get that data into a DataObject using the CsvBullkLoader?
I need to import link name and link from an external database. The database field there stores the data like LinkName (LinkUrl), LInkName (LinkUrl), LInkName (LinkUrl) etc. in one field. There can be none or many LinkName(Links) in that field. I only need to store the LinkName and LinkUrl in Silverstripe. In the template I will use these information and the data shows via this link. So no upload from actual files needed.
What I have done so far is:
I created a ProductImage DataObject with Title for LinkName and LinkUrl for LinkUrl. This has a $has_one relationship with a Product DataObject.
I created the ProductImageAdmin ModelAdmin and ProductImageCsvBulkLoader CsvBulkLoader extensions. With the CsvBulkLoader extension I have the problem not knowing how to map the fields as I don’t have a one to one mapping.
I know I need to somehow involve an array in this, but I don’t know how.
Here is my code so far which would work if I had a one to one mapping:
public $columnMap = array(
'Extra Pictures' =>'->importImageInfos',
'ProductFamily' => 'Product.Title'
);
public static function importImageLink(&$obj, $val, $record){
$imageArray = explode(',', $val);
foreach($imageArray as $subval){
$parts = explode('(', $subval);
$imgName = $parts[0];
$newpart = explode(')', $parts[1]);
$imgUrl = $newpart[0];
$obj->Title = $imgName;
$obj->ImageLink = $imgUrl;
}
public $relationsCallbacks = array(
'Product.Title' => array(
'relationname' => 'Product',
'callback' => 'getProductByTitle'
)
);
public static function getProductByTitle (&$obj, $val, $record){
return Product::get()->filter('Title', $val)->First();
}
public $duplicateChecks = array(
'Extra Pictures' => 'ImageLink'
);
column name: Extra Pictures (luckily it is a column in the middle of the file)
data example in colum field: ER-1.png (https://domain.com/3FSduKOJzR_ER-1.png),ER-2.png (https://domain.com/TzGbAAFNlSxC_ER-2.png),ER-4.jpg (https://domain.com/L9JLcLfRWG_ER-4.jpg)
It is not a one-off import. I need to be able to do it whenever I need to update the data.
This works fine to get one of the images as it gets overwritten, so I need to add somehow an array, but I don’t know how to create this in order to fill my DataObject from the CsvBulkLoader extension.
The more I read about ArrayList, ArrayData and DataObject google it the more I get confused.
I have to say that I am relatively new to PHP and Silverstripe. The solution might be easy but I am so confused that I just don’t get this.
Any help would be much appreciated.
Thank you very much.