gugoan
(Gugoan)
December 31, 2017, 6:49pm
1
I’m not sure how to use the extension “yii2-csv-importer”.
I need to import a CSV file that is in the app/web/uploads/mod-key.csv directory
My table/model (BASE) and csv file have the following structure:
[b]‘id’
‘date’
'Pan
‘key’
‘goal’
‘accomplished’
‘Weight’
‘points’
‘update’[/b]
How would my ACTION look?
use ruskid\csvimporter\<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />;
public function actionImpbase()
{
$importer = new CSVImporter();
$importer->setData(new CSVReader([
'filename' => Yii::$app->request->baseUrl."/uploads/mod-key.csv",
'tableName' => Base::tableName(),
'fgetcsvOptions' => [
'delimiter' => ';'
]
]));
return $this->redirect(['index']);
}
alirz23
(Alirz23)
January 1, 2018, 3:07am
2
the documentation is pretty clear they have very comprehensive examples, you have a creator and a reader you need to run import method with your schema and with callbacks to locate the data column
$importer = new CSVImporter();
$importer->setData(new CSVReader([
'filename' => Yii::$app->request->baseUrl."/uploads/mod-key.csv",
'tableName' => Base::tableName(),
'fgetcsvOptions' => [
'delimiter' => ';'
]
]));
$numberRowsAffected = $importer->import(new MultipleImportStrategy([
'tableName' => ModelName::tableName(), // change your model names accordingly
'configs' => [
[
'attribute' => 'id',
'value' => function($line) {
return $line[0];
}
],
[
'attribute' => 'date',
'value' => function($line) {
return $line[1];
}
]
// put your remaining columns here
],
]));