ruskid/yii2-csv-importer
Composer 安装命令:
composer require ruskid/yii2-csv-importer
包简介
Helper to import csv files into database
README 文档
README
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 适用场景与选型建议
ruskid/yii2-csv-importer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 366.19k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2015 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「csv」 「importer」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ruskid/yii2-csv-importer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ruskid/yii2-csv-importer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ruskid/yii2-csv-importer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
The Central Bank of the Russian Federation currency importer
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
The plugin will be used for an importing an entries from a CSV file.(Supported field type: Plain Text, Email, Radio Buttons, Dropdown, URL.)
统计信息
- 总下载量: 366.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 18
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2015-09-21