le0m/yii2-import 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 le0m/yii2-import 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-05-04