Adding Custom CsvBulkUploader to ModelAdmin in Silverstripe

╄→гoц情女王★ 提交于 2019-12-24 11:48:03

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/44271755/adding-custom-csvbulkuploader-to-modeladmin-in-silverstripe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!