le0m/yii2-import
Composer 安装命令:
composer require le0m/yii2-import
包简介
An extension for the Yii2 framework to import data from files
关键字:
README 文档
README
This extension helps you import data from files like CSV or JSON into your application.
Installation
The preferred way to install this extension is through composer.
Either run
composer require le0m/yii2-import
or add
"le0m/yii2-import": "*"
to the require section of your composer.json.
Usage
This extension provide support to import data from files to data structures like array or ActiveRecord.
First you must create an Importer, which needs a reader and an importStrategy to work, then you can import:
$importer = new Importer([ // this can be an array of strings to use as column names for the imported data, // `true` if the column names can be extracted from the file itself (the 'how' is specific to the implementation) // or `false` to disable and not use column names (default). 'columnNames' => [...], // see below for how to setup this 'reader' => [ 'class' => '\common\components\importer\CsvReader', ], // see below for how to setup this 'importStrategy' => [ 'class' => '\common\components\importer\ARImportStrategy', ] ]); // get the imported data $data = $importer->import();
The importer will read the file using the chosen reader implementation, passing each element to the chosen importStrategy implementation. The returning value is an array of imported data.
If the importStrategy return false on a single element, the Importer will ignore it.
By default this extension provides two reader (CSV and JSON) and two import strategy (to array and to ActiveRecord) implementations to choose from, but you can add your own by implementing the appropriate interface.
Import a CSV file
The CsvReader accepts the following properties:
'reader' => [ 'class' => '\common\components\importer\CsvReader', // limit the max length of a single line to read from file // 0 means no limit 'lineLength' => 0, // character used to separate values on a single line 'valueDelimiter' => ",", // character used to enclose values 'valueEnclosure' => '"', // character used as escape in the values 'escapeCharacter' => "\\", ]
Import a JSON
The JsonImporter has no particular properties:
'reader' => [ 'class' => '\common\components\importer\JsonReader', ]
Import to ActiveRecord
The ARImportStrategy accepts the following properties:
'importStrategy' => [ 'class' => '\common\components\importer\ARImportStrategy', // name of the ActiveRecord class to use for the import 'className' => '...', // if you need custom code to handle loading data read from the file to the AR // (ex. column names from the file are different than property names from the AR // this function will be called for each element in the original file, // return false to not import the current element ([[Importer]] ignores false elements) 'loadProperties' => function ($model, $data) { ... }, // whether to automatically save the AR at the end of the import // AR with validation errors are returned anyway 'saveRecord' => true, ]
le0m/yii2-import 适用场景与选型建议
le0m/yii2-import 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「csv」 「import」 「activerecord」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 le0m/yii2-import 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 le0m/yii2-import 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 le0m/yii2-import 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Parse use statements for a reflection object
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Tool for copying data from a production database to a dev database. Also useful for making backups of production databases.
A fork of konnco/filament-import with support of Laravel 11 since the default importer of Filament 3 is nonsense for basic use case.
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-05-04