2014-09-28 05:59:48 +02:00
title: Import CSV Data through a Controller
# Import CSV Data through a Controller
2016-03-30 02:17:28 +02:00
You can have more customised logic and interface feedback through a custom controller. Let's create a simple upload
2014-09-28 05:59:48 +02:00
form (which is used for `MyDataObject` instances). You can access it through
`http://yoursite.com/MyController/?flush=all` .
2017-08-03 02:51:32 +02:00
```php
2017-08-07 05:11:17 +02:00
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\Control\Controller;
class MyController extends Controller
{
private static $allowed_actions = [
'Form'
];
protected $template = "BlankPage";
public function Link($action = null)
{
return Controller::join_links('MyController', $action);
}
public function Form()
{
$form = new Form(
$this,
'Form',
new FieldList(
new FileField('CsvFile', false)
),
new FieldList(
new FormAction('doUpload', 'Upload')
),
new RequiredFields()
);
return $form;
}
public function doUpload($data, $form)
{
$loader = new CsvBulkLoader('MyDataObject');
$results = $loader->load($_FILES['CsvFile']['tmp_name']);
$messages = [];
if($results->CreatedCount()) {
$messages[] = sprintf('Imported %d items', $results->CreatedCount());
}
if($results->UpdatedCount()) {
$messages[] = sprintf('Updated %d items', $results->UpdatedCount());
}
if($results->DeletedCount()) {
$messages[] = sprintf('Deleted %d items', $results->DeletedCount());
}
if(!$messages) {
$messages[] = 'No changes';
}
$form->sessionMessage(implode(', ', $messages), 'good');
return $this->redirectBack();
}
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-09-28 05:59:48 +02:00
< div class = "alert" markdown = "1" >
2017-07-03 03:22:12 +02:00
This interface is not secured, consider using [Permission::check() ](api:SilverStripe\Security\Permission::check( )) to limit the controller to users with certain
2014-09-28 05:59:48 +02:00
access rights.
< / div >