Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Adding relations to ComponentSet only saves last one

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 981 Views
  • MDrollette
    Avatar
    Community Member
    10 Posts

    Adding relations to ComponentSet only saves last one Link to this post

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

    981 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.