popov/php-importer

适用于不同格式的通用快速导入表,例如Excel或CSV

0.1.2 2016-08-30 10:11 UTC

This package is auto-updated.

Last update: 2024-08-29 05:06:09 UTC


README

适用于不同表格格式(如Excel或CSV)的通用导入器

安装

使用composer安装

composer require popov/php-importer -o

支持的驱动器

  1. LibXl (商业)
  2. Excel
  3. Soap
  4. Csv(尚未实现)

要求

导入器在后台使用INSERT ... ON DUPLICATE KEY UPDATE 语法以减少对数据库的查询次数。

您的表中应只有一个唯一字段,否则您可能会得到不希望的结果。如果您需要有几个唯一字段,您应使用UNIQUE 约束将它们分组,例如UNIQUE (field_1, field_2, ...)

用法

示例导入文件

独立使用

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());
}

高级用法

大多数流行的PHP框架实现了IoC模式,并且它们也实现了标准接口Interop\Container\ContainerInterface。此库支持此功能。您可以将自己的IoC传递给Factory并享受创建对象。

$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);

配置

带有*标记的选项是必需的。

driver *

驱动器是处理来自source的数据的处理程序。您可以使用已注册的驱动器或创建自己的驱动器。

['driver' => 'Excel']

driver_options

您可以将任何自定义选项传递给驱动器,没有任何限制。

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

要处理的文件的路径。

sheet: name

要处理的表单的名称。默认情况下,取第一个表单。

sheet: skip

跳过文件中的前N行。默认情况下,取第一行。

###fields 从一个资源映射字段到新的(MySQL,CSV,Excel)

最简单的映射可以写成

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

字段过滤和准备可以分组在链中

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

__filter - 保留名称用于过滤

__prepare - 保留名称用于准备

所有保留选项都以前缀"__"(双下划线)开始。

__table

'__table' => 'table_name',

必需。保存导入数据所在的表。

__codename

'__codename' => 'discount',

必需。与表相关的配置的快捷唯一名称。

__identifier

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

用于避免重复项目的唯一字段名称。标识符可以是单个字段也可以是多个字段。

__ignore

'__ignore' => ['comment'],

在保存操作中应忽略的字段。这些字段可用于数据过滤。

__exclude

'__exclude' => false,

布尔值。从保存操作中排除表。所有字段均可用于数据过滤。

__exclude

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

此选项仅在配置中设置至少两组字段时适用。例如,如果您有客户信息和评论信息,您将客户信息放在字段的第一组中,评论信息放在字段的第二组中。当第一组将被保存时,ID将在内存中标记,第二组可以使用此值。

选项

mode

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

save - 保存新数据和已存在数据

update - 仅更新已存在数据

与ZF2集成

为此有一个模块