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.
Posted to: Import CSV with $belongs_many_many records | Show Thread | Post Reply