Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Adding relations to ComponentSet only saves last one


Reply


1010 Views

Avatar
MDrollette

Community Member, 10 Posts

14 November 2009 at 9:07am

Edited: 14/11/2009 9:08am

I am using a CsvBulkLoader to import a ton of DataObjects. These objects have a has_many relation of photo's. In the csv source the photos are stored as filenames paths in seperate fields. I have this method that gets called on each of those fields to hopefully add the photos to the has_many relation. It almost works, but it seems to only save the very last photo... any ideas why it doesn't append the photo objects ?

class CarPartCsvBulkLoader extends CsvBulkLoader {
public $columnMap = array(
'dvid' => 'DVID',
'vehicle_price' => 'Price',
'vehicle_year' => 'Year',
'popup_vehicle_a' => '->importPhoto',
'popup_vehicle_b' => '->importPhoto',
);

static function importPhoto(&$obj, $val, $record) {
$existingPhotos = $obj->Photos();

$filename = 'assets/CarPartImages/'.$val;
$file = DataObject::get_one('File', "Filename = '{$filename}'");
if($file){
$photo = new Photo();
$photo->ImageID = $file->ID;
$existingPhotos->add($photo);
}
}