定制 pmatseykanets/artisan-io 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

pmatseykanets/artisan-io

Composer 安装命令:

composer require pmatseykanets/artisan-io

包简介

Artisan data import command for Laravel

README 文档

README

StyleCI tests Latest Stable Version License

This package adds data import capability to your Laravel project. It contains an artisan command import:delimited which allows you, as the name implies, to import delimited data (CSV, TSV, etc) into your local or remote database.

Main features:

  • Supports multiple database connections (defined in config\database.php).
  • You can use either a table name or Eloquent model class to import your data. By using Eloquent model you can benefit from mutators and accessors.
  • Import modes:
    • insert
    • insert-new
    • update
    • upsert
  • Row validation rules

If you find this package usefull, please consider bying me a coffee.

Buy Me a Coffee at ko-fi.com

Installation

You can install the package via composer:

composer require pmatseykanets/artisan-io

If you're using Laravel < 5.5 or if you have package auto-discovery turned off you have to manually register the service provider:

// config/app.php
'providers' => [
    ...
    ArtisanIo\ArtisanIoServiceProvider::class,
],

Alternatively you can register the command yourself

Open app\Console\Kernel.php in the editor of your choice and add the command to the $commands array

protected $commands = [
    \ArtisanIo\Console\ImportDelimitedCommand::class,
];

Usage

php artisan import:delimited --help

Usage:
  import:delimited [options] [--] <from> <to>

Arguments:
  from                           The path to an import file i.e. storage/import.csv
  to                             The table or Eloquent model class name

Options:
  -f, --fields[=FIELDS]          A comma separated list of field definitions in a form <field>[:position] i.e. "email:0,name,2". Positions are 0 based
  -F, --field-file[=FIELD-FILE]  Path to a file that contains field definitions. One definition per line
  -m, --mode[=MODE]              Import mode [insert|insert-new|update|upsert] [default: "upsert"]
  -k, --key[=KEY]                Field names separated by a comma that constitute a key for update, upsert and insert-new modes
  -R, --rule-file[=RULE-FILE]    Path to a file that contains field validation rules
  -d, --delimiter[=DELIMITER]    Field delimiter [default: ","]
  -i, --ignore[=IGNORE]          Ignore first N lines of the file
  -t, --take[=TAKE]              Take only M lines
  -c, --database[=DATABASE]      The database connection to use
  -x, --transaction              Use a transaction
      --dry-run                  Dry run mode
      --no-progress              Don't show the progress bar
      --force                    Force the operation to run when in production

Examples

Lets say we have employee.csv file

email,firstname,lastname,employed_on,phone
john.doe@example.com,John,Doe,07/01/2014,2223334455
jane.doe@example.com,Jane,Doe,02/15/2015,5554443322

table employee the migration for which may look like

Schema::create('employees', function (Blueprint $table) {
    $table->increments('id');
    $table->string('email')->unique();
    $table->string('firstname', 60)->nullable();
    $table->string('lastname', 60)->nullable();
    $table->string('phone', 10)->nullable();
    $table->date('employed_on')->nullable();
    $table->timestamps();
});

and model \App\Employee

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    protected $table = 'employees';

    protected $fillable = [
        'email',
        'firstname',
        'lastname',
        'phone',
        'employed_on'
    ];
}

Insert

If employees table is empty and you'd like to populate it

php artisan import:delimited employee.csv "\App\Employee" -f email:0,firstname:1,lastname:2,phone:4,employed_on:3 -m insert

Note: The buity of using Eloquent model in this case is that timestamps created_at and updated_at will be populated by Eloquent automatically.

Upsert

Now let's assume John's record is already present in the table. In order to update Jon's record and insert Jane's one you'd need to cnahge the mode and specify key field(s).

php artisan import:delimited employee.csv "\App\Employee" -f email:0,firstname:1,lastname:2,phone:4,employed_on:3 -m upsert -k email

Update

If you want to just update phone numbers for existing records

php artisan import:delimited employee.csv "\App\Employee" -f email:0,phone:4 -m update -k email

Field definition file

Each field definition goes on a separate line in the format

<fieldname>[:position]

where position is an ordinal position of the field in the data file. The position is 0-based and can be omitted.

Example employee.fld

email:0
firtname:1
lastname:2
phone:4
employed_on:3

Row validation rules file

A row validation rule file is simply a php file that returns an array of rules. You can any of the available Laravel validation rules

Example employee.rule

<?php

return [
    'email' => 'required|email',
    'firstname' => 'string|min:2|max:60',
    'lastname' => 'string|min:2|max:60',
    'phone' => 'digits:10|regex:/[2-9][0-9]{2}[2-9][0-9]{6}/'
    'employed_on' => 'date_format:m/d/Y|after:2010-07-15|before:'.date('Y-m-d', strtotime('tomorrow'));
];

License

The artisan-io is open-sourced software licensed under the MIT license

pmatseykanets/artisan-io 适用场景与选型建议

pmatseykanets/artisan-io 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.97k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2015 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 pmatseykanets/artisan-io 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-21