yii2-csv-importer

Yii2 Excel helper to import csv files to tables

  • 所有者: ruskid/yii2-csv-importer
  • 平台:
  • 许可证: GNU General Public License v2.0
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Yii2 CSV Importer to Database

Helper for CSV imports to tables.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist ruskid/yii2-csv-importer "dev-master"

or add

"ruskid/yii2-csv-importer": "dev-master"

to the require section of your composer.json file.

Usage

$importer = new CSVImporter;

//Will read CSV file
$importer->setData(new CSVReader([
    'filename' => $this->file->tempName,
    'fgetcsvOptions' => [
        'delimiter' => ';'
    ]
]));

//Import multiple (Fast but not reliable). Will return number of inserted rows
$numberRowsAffected = $importer->import(new MultipleImportStrategy([
    'tableName' => VendorSwType::tableName(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[1];
            },
            'unique' => true, //Will filter and import unique values only. can by applied for 1+ attributes
        ]
    ],
]));

//Import Active Records (Slow, but more reliable). Will return array of primary keys
$primaryKeys = $importer->import(new ARImportStrategy([
    'className' => BusinessType::className(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[2];
            },
        ],
        [
            'attribute' => 'items',
            'virtual' => true, //set non AR DB attribute (behavior or public model attribute) 
            'value' => function($line) {
                return Item::find()->select(['id'])->column('id');
            },
        ]
    ],
]));


// More advanced example. You can use queries to set related data.
// Use query caching for performance
$importer->import(new MultipleImportStrategy([
    'tableName' => ProductInventory::tableName(),
    'configs' => [
        [
            'attribute' => 'product_name',
            'value' => function($line) {
                //You cand perform your filters and excludes here. Empty exclude example:
                return $line[7] != "" AppHelper::importStringFromCSV($line[7]) : null;
            },
        ],
        [
            'attribute' => 'id_vendor_sw_type',
            'value' => function($line) {
                $name = AppHelper::importStringFromCSV($line[1]);
                $vendor = VendorSwType::getDb()->cache(function ($db) use($name) {
                    return VendorSwType::find()->where(['name' => $name])->one();
                });
                return isset($vendor) ? $vendor->id : null;
            },
        ],   
    ],
]));

//Special case only available with Active Record Strategy.
//Get primary key list of new imported items for later use.
$primaryKeys = $importer->import(new ARImportStrategy([
    'className' => Fabrica::className(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[0];
            },
        ]
    ],
]));

//You can use the primary key list for the next import of related data.
//The order of primary key items will be the same as in csv file.
$importer->import(new MultipleImportStrategy([
    'tableName' => Product::tableName(),
    'configs' => [
        [
            'attribute' => 'id_fabrica',
            'value' => function($line) use (&$primaryKeys) {
                return array_shift($primaryKeys);
            },
            'unique' => true,
        ],     
    ],
]));


//You can skip CSV Row / Active Record import if some csv row doesn't meet the requirements.
$importer->import(new MultipleImportStrategy([
    'tableName' => ProductCategory::tableName(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[0];
            },
        ],
        [
            'attribute' => 'description',
            'value' => function($line) {
                return $line[1];
            },
        ],
        [
            'attribute' => 'estado',
            'value' => function($line) {
                return $line[2];
            },
        ]
    ],
    //All ACTIVE categories that don't have name set to 'MS_SQLSERVER' or empty will be imported.
    'skipImport' => function($line){
        if($line[0] == 'MS_SQLSERVER', $line[0] == ""){
            return true;
        }
        if($line[2] == 'NOT ACTIVE'){
            return true;
        }
    }           
]));

//Import or update multiple (Fast but not reliable). Will return number of inserted, updated and unchanged rows
//ARUpdateStrategy can be used instead of MultipleUpdateStrategy if you want to use AR validation.
//The returned value will be the number of inserted, updated and unchanged rows in both cases.
$records = $importer->import(new MultipleUpdateStrategy([
	'className' => Customer::className(),
	'csvKey' => function ($line) {
		return $line[0];
	},
	'rowKey' => function ($row) {
		return $row['gecom_id'];
	},
	'skipImport' => function ($line) {
		return !$line[0];
	},
	'configs' => [
		[
			'attribute' => 'customer_id',
			'value' => function($line) {
				return $line[0];
			},
		],
	],
]));

主要指标

概览
名称与所有者ruskid/yii2-csv-importer
主编程语言PHP
编程语言PHP (语言数: 1)
平台
许可证GNU General Public License v2.0
所有者活动
创建于2015-08-20 13:29:53
推送于2019-05-23 12:13:43
最后一次提交2019-05-23 14:13:42
发布数1
最新版本名称v1.0.0 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数36
关注者数12
派生数15
提交数41
已启用问题?
问题数9
打开的问题数4
拉请求数5
打开的拉请求数0
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?