1

我不确定如何使用扩展名“yii2-csv-importer”。我需要导入app/web/uploads/mod-key.csv目录中的 CSV 文件

我的表/模型(BASE)和 csv 文件具有以下结构:

'id'
'date'
'Pan
'key'
'goal'
'accomplished'
'Weight'
'points'
'update'

我的 ACTION 会是什么样子?

更新

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
],
]));
4

1 回答 1

1
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
],
]));
于 2018-04-22T19:48:49.767 回答