我不确定如何使用扩展程序“yii2-csv-importer”。 我需要导入位于 app / web / uploads / mod-key.csv 目录中的CSV文件
我的表/模型(BASE)和csv文件具有以下结构:
'id'
'date'
'Pan
'key'
'goal'
'accomplished'
'Weight'
'points'
'update'
我的行动怎么样?
更新
use ruskid\csvimporter\CSVImporter;
use ruskid\csvimporter\CSVReader;
use ruskid\csvimporter\ImportInterface;
use ruskid\csvimporter\MultipleImportStrategy;
use ruskid\csvimporter\BaseImportStrategy;
$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
],
]));
答案 0 :(得分:0)
use ruskid\csvimporter\CSVImporter;
use ruskid\csvimporter\CSVReader;
use ruskid\csvimporter\ImportInterface;
use ruskid\csvimporter\MultipleImportStrategy;
use ruskid\csvimporter\BaseImportStrategy;
$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
],
]));