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

1004 Views

Avatar
MDrollette

14 November 2009 at 9:07am (Last edited: 14 November 2009 9:08am), Community Member, 10 Posts

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);
}
}