popov/php-importer 问题修复 & 功能扩展

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

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

popov/php-importer

Composer 安装命令:

composer require popov/php-importer

包简介

Universal fast importer tables for different formats, such as Excel or CSV

README 文档

README

Universal importer for different table formats like excel or csv

Installation

Install it with composer

composer require popov/php-importer -o

Supported drivers

  1. LibXl (commercial)
  2. Excel
  3. Soap
  4. Csv (not implemented yet)

Requirements

Importer use INSERT ... ON DUPLICATE KEY UPDATE Syntax in background for reduce number of queries to database.

You should have only one unique field in your table otherwise you can get undesirable result. If you need to have several unique fields you should group them with UNIQUE Constraint such as UNIQUE (field_1, field_2, ...)

Usage

Example import File

Nominal Serial
3% 3002345
3% 3002346
3% 3002346
5% 5002344
5% 5002345

Standalone

use Popov\Importer\Factory\DriverCreator;
use Popov\Importer\Importer;
use Popov\Db\Db;

$config = [
    'tasks' => [
        'discount-card' => [
            'driver' => 'libxl',
            'fields' => [
                [
                    // mapping fields in file to db fields with apply filters
                    'Nominal' => ['name' => 'discount', '__filter' => ['percentToInt']],
                    'Serial' => 'serial',
                    
                    // table where save imported data
                    '__table' => 'discount_card',
                    // shortcut name
                    '__codename' => 'discount',
                    // unique field name for avoid duplicate
                    '__identifier' => 'serial'
                ],
            ],
        ],
    ],
];

$pdo = new PDO('mysql:host=myhost;dbname=mydb', 'login', 'password'); 
$db = (new Db())->setPdo($pdo);

$factory = new DriverCreator($config);
$importer = new Importer($factory, $db);

if ($importer->import('discount-card', '/path/to/file.xls')) {
  echo 'Success import!';
} else {
  var_dump($importer->getErrors());
}

Advanced Usage

Most popular PHP frameworks implement IoC pattern and they also implement standard interface Interop\Container\ContainerInterface. This library support this functionality. You can pass your own IoC to Factory and be happy with creating objects.

$pdo = new PDO('mysql:host=myhost;dbname=mydb', 'login', 'password'); 
$db = (new Db())->setPdo($pdo);

$container = /* getYourContainer */;
$factory = new DriverCreator($config, $container);
$importer = new Importer($factory, $db);

Configuration

Options marked with * are required.

driver *

Driver is handler for data from source. You can use one of the registered drivers or create your own.

['driver' => 'Excel']

driver_options

You can pass any custom options to driver, there is no limit for it.

Excel options
[
    'driver' => 'Excel',
    "driver_options" => [
        "path" => "data/path/to/excel.xlsx",
        "sheet" => [
            "name" => "Sheet Name",
            "skip" => 2,
        ],
    ],
]
path

Path to file which should be handled.

sheet: name

Name of the sheet which should be handled. By default first sheet is taken.

sheet: skip

Skip first N rows in file. By default first row is taken.

###fields Mapping fields from one resource to new (MySQL, CSV, Excel)

The simples mapping can be written as:

// from   =>   to
['Serial' => 'serial']

Fields filtration and preparation can be grouped in chain

[
    'Nominal' => ['name' => 'discount', '__filter' => ['trim', 'percentToInt']]
]

__filter - reserved name for filtration

__prepare - reserved name for preparation

All reserved options begin with "__" (double underscore).

__table

'__table' => 'table_name',

Required. A table where to save imported data.

__codename

'__codename' => 'discount',

Required. Shortcut unique name for config related to table.

__identifier

'__identifier' => 'serial',
// or
'__identifier' => ['asin', 'marketplace'],

Unique field name for avoid duplicated items. Identifier can be as one field such as multiple fields.

__ignore

'__ignore' => ['comment'],

Fields which should be ignored in save operation. These fields can be used in data filtration.

__exclude

'__exclude' => false,

Bool. Exclude table from save operation. All fields can be used in data filtration.

__exclude

'__foreign' => ['customer_table' => 'customerId'],

This option is actual if set up minimum two group of fields in config. For example, if you have customer and review info, you put customer info in first group of fields and review info in second group of fields. When first group will be saved the ID will be marked in memory and second group can use this value.

Options

mode

'__options' => [
    'mode' => 'save'
]

save - save new and excited data

update - only update excited data

Integration with ZF2

There's a module for that!

popov/php-importer 适用场景与选型建议

popov/php-importer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 200 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「excel」 「csv」 「importer」 「agere」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 popov/php-importer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 popov/php-importer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-25