Reputation: 157
after looking at the documentation for the built-in CSV importing, it's still not clear to me how to add a custom CsvBulkUploader to ModelAdmin. I see how you can easily add the default uploader and how you can create a custom controller for importing but it's not clear to me how you would add this to a ModelAdmin. I've spent the morning looking through Stack Overflow and the SilverStripe community forums, but haven't been able to find anything yet. Any direction would be greatly appreciated!
Upvotes: 1
Views: 331
Reputation: 157
I figured it out.
You can add the CSV Bulk Loader to your ModelAdmin by declaring it in $model_importers
:
<?php
class PlayerAdmin extends ModelAdmin {
private static $managed_models = array(
'Player'
);
private static $model_importers = array(
'Player' => 'CsvBulkLoader',
);
private static $url_segment = 'players';
}
?>
And as indicated in the CSV Import documentation, you can extend the CsvBulkLoader
class. For example:
<?php
class PlayerCsvBulkLoader extends CsvBulkLoader {
public $columnMap = array(
'Number' => 'PlayerNumber',
...
);
public $duplicateChecks = array(
'Number' => 'PlayerNumber'
);
public $relationCallbacks = array(
'Team.Title' => array(
'relationname' => 'Team',
'callback' => 'getTeamByTitle'
)
);
public static function getTeamByTitle(&$obj, $val, $record) {
return FootballTeam::get()->filter('Title', $val)->First();
}
}
?>
In the documentation what wasn't made explicit was that you pull in the new extended Bulk Loader by simply adding it to $model_importers
in your ModelAdmin. So now instead of using CsvBulkLoader
, you'll be using PlayerCsvBulkLoader
. The snippet up top would be revised thusly:
<?php
class PlayerAdmin extends ModelAdmin {
private static $managed_models = array(
'Player'
);
private static $model_importers = array(
'Player' => 'PlayerCsvBulkLoader',
);
private static $url_segment = 'players';
}
?>
Fairly simple. I had tried this approach early on, but had misspelled the name of the subclass!
UPDATE: Just added this to SilverStripe's documentation
Upvotes: 2