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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Import CSV with $belongs_many_many records


Reply


3 Posts   249 Views

Avatar
kindlemanwill

Community Member, 68 Posts

5 February 2014 at 7:26pm

Hi

I want to be able to import a csv of locations that belong_many_many services. Both locations and services are dataobjects

In the locations model admin, there is a csv import function, which seems to understand exactly what i want to do and is helpfully suggesting the csv format i should use. The trouble is - I don't understand what it is telling me:

Database columns

Lat
Lat
Lng
Lng
Address
Address
Suburb
Suburb
State
State
Postcode
Postcode
Country
Country
Relations

Services
Service

what should the format of my csv be to get a location with a couple of services imported?

Thanks for any guidance

Will

Avatar
martimiz

Forum Moderator, 1106 Posts

6 February 2014 at 5:01am

At this point I don't think you can csv-import many_many relations out of the box... Maybe someone can prove me wrong, but else you could do something like this:

- in your csv-file create a column called 'Service', containing a comma-separated list of service titles

- extend the CsvBulkLoader to LocationCsvBulkLoader

- tell your ModelAdmin to use the new extension:

   private static $model_importers = array(
    'Location' => 'LocationCsvBulkLoader'
   );

- in your LocationCsvBulkLoader create a function that will add the comma-separated list to each new Location object

   public function linkServicesByTitle(&$obj, $val, $record) {
      $obj->ServiceList = $val;
   }

Again, in your LocationCsvBulkLoader map the csv-file 'Service' column to your function:

   public $columnMap = array(
      ...
      'Service' => '->linkServicesByTitle'
   );

Now in your Locations' onAfterWrite() function, you can use $this->ServiceList to collect the Services with the requested title(s), and add them to your many_many relation, using:

if ($this->ServiceList) {
   ...
   $this->Services()->add($aPageYouFound);
}

Of course this goes for existing services. You could probably build on this by creating/writing Services on the fly, but that would be more complex.

Avatar
kindlemanwill

Community Member, 68 Posts

6 February 2014 at 1:52pm

Ok, cool thanks for that. I had a slight feeling it wouldn't do it automatically and then the spec came up.

I should be able to get it working with those instructions. Thanks very much for your help : )