feijs/model-importer
Composer 安装命令:
composer require feijs/model-importer
包简介
Directly import Eloquent models from excel files
README 文档
README
This package aims to provide a flexible and reusable solution to import Laravel Eloquent model data directly from excel and csv files
Features
- Simple & direct import
- Match column titles to model attributes
- Import related models
- Queued importing for multiple files
Installation
Add the package in composer.json and run composer update
"require": { ... "feijs/model-importer": "dev-master" }
Add the ServiceProvider to the providers in config\app.php
'Feijs\ModelImporter\ModelImporterServiceProvider',
Usage
Importable models
Any model which can be imported should implement Model\ImportableInterface and use Model\ImportableTrait
use Feijs\ModelImporter\Model\ImportableTrait as ImportableModelTrait; class Student extends Eloquent implements ImportableModel { use ImportableModelTrait; }
Attributes
Imported data is matched with existing data on a set of match attributes. These should be returned by the getMatchAttributes method.
public function getMatchAttributes() { return ['student_id', 'phonenumber']; }
These match attributes should be mass assignable, and thus be included in the fillable array
public $fillable = ['student_id', 'phonenumber', '...'];
Any attributes which should be importable, but not distict, should be returned by the getImportAttributes.
These import attributes do not necassarily need to be included in the fillable array.
public function getImportAttributes() { return ['name', 'email', 'street', 'zipcode', 'city']; }
Relations
To import relation data, or link the newly imported models to existing relations, you can specify relations which should be imported.
These should be returned by the getImportRelations method. Note that any models specified here should implement Model\ImportableInterface as well.
The following relations may be imported: HasOne, HasMany, BelongsTo, BelongsToMany.
public function bankAccount(){ return $this->hasMany('BankAccount'); } public function getImportRelations() { return ['bankAccount']; }
Import functionality
Initialisation
$importer = App::make('Feijs\ModelImporter\ModelImporter'); $importer->setModel('Student');
CSV Import Settings
You may override the csv import settings and file encoding with the setSettings method.
$importer->setSettings([ 'csv' => [ 'enclosure' => '"', //Default 'delimiter' => "," //Default ], 'encoding' => 'UTF-8' //Default ]);
Input
Input for the importer should include at least:
file(Symfony\Component\HttpFoundation\File\UploadedFile),overwrite: Update existing data? (boolean)model: an array of model attribute -> column title translationsdefaults: an array of models -> attributes -> default value
All model names, attributes and column names should be in spinal-case (slugs)
Importing a single file
$success = $importer->import(Input::all()); //Equivalent with example input: $success = $importer->import([ 'file' => Input::file('data.csv'), 'overwrite' => false, 'student' => [ 'student_id' => 'studentnumber', 'phonenumber' => 'mobile', 'city' => 'city', ], 'defaults' => [ 'student' => [ 'city' => 'Amsterdam' ] ] ]);
Importing multiple files
To import multiple files from a single request, use the Distributor class and the importFiles method. This will queue each file for import. Results will be written to the log.
- The first parameter is an array with files
- The second parameter is the same input array as on single file import (except the file)
- The third parameter is an array with csv & encoding settings
- The fourth parameter is the classname of the model which is to be imported
$distributor = App::make('Feijs\ModelImporter\Queue\Distributor;'); $jobs_queued = distributor->importFiles( 'file' => Input::file('files'), Input::except('csv', 'encoding', 'files'), Input::only('csv', 'encoding'), 'Student' );
Output
Input Validation
To get any validation errors (for the model-importer input) call the validationErrors method
$importer->validationErrors()
Model validation
To get errors encountered during the import (ex. from in-model validation) call the errors method
$importer->errors()
Success
To find the number of data lines which were succesfully imported, call the getImported method
$importer->getImported()
Customization
Model input slug
By default the input should include the slugged model classname. To change this, override the getPrefix method
public function getPrefix() { return snake_case(class_basename(get_class($this))); //default }
Dates
To change which attributes should be parsed as Carbon objects, override the isDateAttribute function
public function isDateAttribute($key) { return in_array($key, $this->getDates()); //default }
feijs/model-importer 适用场景与选型建议
feijs/model-importer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 feijs/model-importer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 feijs/model-importer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 30
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2015-07-17