lucasguo/yii2-import
Composer 安装命令:
composer require lucasguo/yii2-import
包简介
Simple import module for yii2 framework
README 文档
README
Yii2-import is an extension for Yii Framework. It helps you transfer file into models. Currently support Excel format only. But it's easily to extend to support your file.
Installation
You can use composer to install this extension by:
- run
$ php composer.phar require lucasguo/yii2-import "*"
- or add
"lucasguo/yii2-import": "*"
to your composer.json and then execute 'composer update'.
Usage
Assume there is a model class
class Post extends Model { const STATUS_NEW = 0; const STATUS_APPROVED = 1; public $title; public $status; public $content; public static function getStatusList() { return [ self::STATUS_NEW = 'New', self::STATUS_APPROVED = 'Approved', ]; } }
And the form and view as below.
class ImportForm extends Model { public $file; public function rules() { return [ ['file', 'file', 'skipOnEmpty' => false, 'extensions' => 'xlsx'], ]; } }
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <?= $form->field($model, 'file')->fileInput() ?> <div class="form-group"> <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
User have uploaded one Excel file as below.
| Some Description 1 | ||
|---|---|---|
| Some Description 2 | ||
| Title | Status | Content |
| Post 1 | New | Content 1 |
| Post 2 | Approved | Content 2 |
| Approved | Content 3 | |
| Post 3 | New | Content 4 |
Now you can use below code in the controller to help you get the file into models.
$uploadFile = UploadedFile::getInstance($model, 'file'); $importer = new Importer([ 'file' => $uploadFile, 'modelClass' => Post::className(), 'skipRowsCount' => 3, // description lines and header lines should be skipped 'columnMappings' => [ [ 'attribute' => 'title', 'required' => true, // if set this to true, the row that missing this value will be skipped. As in the example line 6 will be skipped ], [ 'attribute' => 'status', 'translation' => function($orgValue, $rowData) { return array_search($orgValue, Post::getStatusList()); }, // this function help fill the status like '0' instead of 'New' ] 'content', ], 'validateWhenImport' => true, //if set this attribute to true, importer will help you validate the models and report the validation errors by $importer->validationErrors ]); try { $posts = $importer->import(); } catch (InvalidFileException $e) { $model->addError('file', 'Invalid import file.'); }
Now you have your models in $posts. The structure of $posts will be
[ 4 => Post{title='Post 1', status=0, content='Content 1'}, 5 => Post{title='Post 2', status=1, content='Content 2'}, 7 => Post{title='Post 3', status=0, content='Content 4'}, ]
Also, you can check imported rows number by $importer->importRows, which is an array of successful imported rows number.
You can notify user about the import result as below.
Yii::$app->session->setFlash("success", count($importer->getImportRows()) . ' rows had been imported'); foreach ($importer->getValidationErrors() as $lineno => $errors) { foreach ($errors as $attribute => $errorMessages) { $error = $errorMessages[0]; break; } Yii::$app->session->addFlash("error", 'Line ' . $lineno . ' has error: ' . $error); }
lucasguo/yii2-import 适用场景与选型建议
lucasguo/yii2-import 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.67k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「import」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lucasguo/yii2-import 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lucasguo/yii2-import 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lucasguo/yii2-import 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Parse use statements for a reflection object
Tool for copying data from a production database to a dev database. Also useful for making backups of production databases.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
A fork of konnco/filament-import with support of Laravel 11 since the default importer of Filament 3 is nonsense for basic use case.
统计信息
- 总下载量: 5.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-11-23