承接 quince/data-importer 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

quince/data-importer

最新稳定版本:v1.0.1

Composer 安装命令:

composer require quince/data-importer

包简介

Tool for importing data from tabular data files into laravel models

README 文档

README

Tool for importing data from tabular data files into laravel models

Installation

Add data-importer to your composer.json file.

for laravel 4:

"require": {
  "quince/data-importer": "~1.0"
}

and for laravel 5:

"require": {
  "quince/data-importer": "~2.0"
}

Use composer to install this package.

$ composer update

Registering the Package

Register the service provider within the providers array found in app/config/app.php:

'providers' => array(
    'Quince\DataImporter\DataImporterServiceProvider'
)

Configuration

Next step is to publish package configuration by running:

in laravel 4 run:

$ php artisan config:publish quince/data-importer

and in laravel 5 run:

$ php artisan vendor:publish

Config file could be found in app/config/packages/quince/data-importer/config.php for laravel 4 and in config/quince/data-importer.php

Usage

You can use data-importer in two ways

Using Laravel IoC Container

Make sure exporter service manager is registered in your app config.

<?php

// Initialize required data
$filePath = '/absolute/path/to/file/to/import';
$bindingModel = '/Namespace/ToYour/Model'; // or YourModel::class

// get data-importer out of IoC
$importer = App::make('importer');

/** @var RowsCollection $data */
$importer->import($filePath, $bindingModel, function($data) {
	// whatever you want to do with data
	// $data is an instance of RowsCollection
	// for example
	
	/** @var RowData $row */
	foreach ($data as $row) {
	    YourModel::create($row->getBase());
	}
});

Using Laravel Dependency Injection

You can also inject Quince\DataImporterManager in your class, and use it.

<?php

use Quince\DataImporterManager as Importer;

class ExampleClass {

	protected $importer

	public function __construct(Importer $importer)
	{
		$this->importer = $importer;
	}

	public function exampleMethod($file)
	{
		$this->importer->import($file->getPath(), DesiredModel::class, function($data) {
			// whatever you want to do with data
		});
	}

}

Advanced Usages

Each row of file is a presenter of a record in database, and each record may have relation records. In your csv file, a column may be present these relation records. Header of this column is a concatenation of relation name and field name of relation that is provide in that column (Concat with relation_joint is set in config file). if a column have many relation records, values should be separated with relation_value_delimiter which is set in config file.

Example

The file below:

name username email phones.number
John Doe Johny J.Doe@mail.com 0941120773
Jane Doe Jane6 miss.J.Doe@gmail.com 0929339687|0916740160

will be converted to something like this (as json):

[
	{
		"name": "John Doe",
		"username": "Johny",
		"email": "J.Doe@mail.com",
		"phones": [
			{
				"number": "0941120773"
			}
		]
	},
	{
		"name": "Jane Doe",
		"username": "Jane6",
		"email": "miss.J.Doe@gmail.com",
		"phones": [
			{
				"number": "0929339687"
			},
			{
				"number": "0916740160"
			}
		]
	}
]

Additional Fields

When only some of the table fields are provided in given file, you can set those field manualy

NOTE: You should specify additional fields are for main record or relation records

<?php

// Initialize required data
$filePath = '/absolute/path/to/file/to/import';
$bindingModel = '/Namespace/ToYour/Model'; // or YourModel::class
$additionalFields = [
	'base' => [
		'column_name' => 'value'
	],
	'relation' => [
		'relation_name' => [
			'column_name' => 'value'
		]
		// another relations goes heare
	]
];

// get data-importer out of IoC
$importer = App::make('importer');

$importer->setAdditionalFields($additionalFields)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });

If fields that specified as additional fields exist in csv file, data will be loaded from csv file. But if you pass true as second parameter for setAdditionalFields aditional fields will overwrite existing data in csv file.

$importer->setAdditionalFields($additionalFields, true)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });

Translating file headers

When column headers of your file are defferent from your table columns, you can map them to your database table columns. Assume your file is like:

Real Name Nickname Mail Address Phone Numbers
John Doe Johny J.Doe@mail.com 0941120773
Jane Doe Jane6 miss.J.Doe@gmail.com 0929339687|0916740160

you can pass a dictionary for your file custom header to translate them to their table-column names.

<?php

// Initialize required data
$filePath = '/absolute/path/to/file/to/import';
$bindingModel = '/Namespace/ToYour/Model'; // or YourModel::class
$dictionary = [
	'Real Name'		=> 'name',
	'Nickname'		=> 'username',
	'Mail Address'	=> 'email',
	'Phone Numbers'	=> 'phones.number'
];

// get data-importer out of IoC
$importer = App::make('importer');

$importer->setDictionary($dictionary)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });

you can also map a single column of your file to a column in your database table.

$importer->setColumnDictionary($columnName, $translation)
         ->import($filePath, $bindingModel, function($data) {
         	// whatever you want to do with data
         });

For more information about advanced usage visit wiki pages

Config file

field description default value
delimiter The character is used to separate each column in a row. ,
enclosure The character is used to specify the enclosure. "
escape The character to escape invalid characters. \
chunk_size The size of rows to be processed by closure you pass to import method. 100
relation_joint The character to join relation name and column of relation table you want to import. .
relation_value_delimiter The character to delimit relation value from each other. |

quince/data-importer 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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