crockett/csv-seeder
Composer 安装命令:
composer require crockett/csv-seeder
包简介
Database seeding using CSV files
README 文档
README
This package is intended to minimize the time and hassle spent importing CSV-based data. By making a few assumptions about your average CSV file, most users won't need any configuration to start seeding. For those that do, the available configuration options offer plenty of control over your CSV and how the data is inserted into the database.
Overview
Key Features
- Automatic mapping of CSV headers to the columns in your DB table.
- Aliases allow you to easily adjust a CSV column's name before it's inserted.
- Insert callbacks can be used to directly manipulate the CSV data before it's inserted.
- ORM support - when using a model, common attributes such as
$guardedand$fillableare applied to the CSV.
Installation
Require this package in your composer.json and run composer update
"crockett/csv-seeder": "1.1.*"
Or just require it directly composer require crockett/csv-seeder
Setup
Here is a typical, single CSV seeder setup:
use Crockett\CsvSeeder\CsvSeeder;
class UsersTableSeeder extends CsvSeeder {
public function __construct()
{
$this->filename = base_path('path/to/csv/users.csv');
$this->table = 'users';
}
public function run()
{
// runs the seeder - alternatively, you could call $this->runSeeder(); for the same result
parent::run();
}
}
If you want to seed multiple CSVs in the same seeder, you could do something like this:
use Crockett\CsvSeeder\CsvSeeder;
class UsersTableSeeder extends CsvSeeder {
public function run()
{
// seed the users table
$this->filename = base_path('path/to/csv/users.csv');
$this->table = 'users';
parent::run();
// seed the posts table
$this->filename = base_path('path/to/csv/posts.csv');
$this->table = 'posts';
parent::run();
}
}
As you can imagine, that can get messy very fast. Instead, you could use the helper method seedFromCSV() which is just a cleaner way to define your parameters and run the seeder in one go:
use Crockett\CsvSeeder\CsvSeeder;
class UsersTableSeeder extends CsvSeeder {
public function run()
{
// seed the users table
$this->seedFromCSV(base_path('path/to/users.csv'), 'users');
// seed the posts table
$this->seedFromCSV(base_path('path/to/posts.csv'), 'posts');
}
}
Usage Examples
Given the following CSV and database table:
// users.csv
first_name,last_name,birth_date,password,favorite_color
Joe,Bar,2000-02-10,joePassword,red
Jim,Foo,1990-02-10,jimPassword,blue
Foo,Bar,1980-02-10,fooPassword,green
// users DB table
id, first_name, last_name, birth_date, password, favorite_color
You can run the seeder with no further setup:
$this->seedFromCSV(base_path('path/to/users.csv'), 'users');
You could even go a step further and omit the table name, as the CSV filename is the same as the table name. CsvSeeder will automatically try to resolve table and column names when they're not defined. If your CSV doesn't have a header row, you'll need to manually define a $mapping, as described in the next section.
Mismatched columns
Unless you have complete control over your CSVs, the headers won't always match up with your DB columns. For example:
// users.csv
first_name, last_name, birth_date, password, favorite_color
// users DB table
id, first_name, last_name, age, password
In this case, you can define $aliases to rename the birth_date column to age before it's inserted:
$this->aliases = [
'birth_date' => 'age'
];
$this->seedFromCSV(base_path('path/to/users.csv'), 'users');
Alternatively, you can manually define a $mapping for your CSV. A mapping allows you to explicitly choose and rename CSV columns. For example:
// users.csv
first_name, last_name, birth_date, password, favorite_color
// users DB table
id, first_name, last_name, color, password
// users seeder
$this->mapping = [
0 => 'first_name',
1 => 'last_name',
3 => 'password',
4 => 'color', // renamed from favorite_color
];
$this->seedFromCSV(base_path('path/to/users.csv'), 'users');
When you define a $mapping, a header row on your CSV is not required. In all other cases, CsvSeeder will assume your header row is the first row after $offset_rows.
Insert Callbacks
In some cases, you'll need to manipulate the CSV data directly before it's inserted to the database. Using an $insert_callback, it couldn't be easier! Everytime a $chunk of rows is read from the CSV, it's passed to the default $insert_callback. All you need to do is define your own callback to override it.
Here we'll iterate over individual rows in the chunk and insert them using Model::create():
$this->insert_callback = function ($chunk) {
foreach($chunk as $row) {
\App\User::create($row->toArray());
}
};
$this->seedFromCSV(base_path('path/to/users.csv'), 'users');
Note, $chunk and $row are instances of \Illuminate\Support\Collection so you can easily manipulate and filter the rows and columns:
$this->insert_callback = function ($chunk) {
foreach($chunk as $row) {
$user_data = $row->only('first_name', 'last_name', 'password')->toArray();
\App\User::create($user_data);
}
};
$this->seedFromCSV(base_path('path/to/users.csv'), 'users');
Configuration
table(string) Database table to insert into.model(string) Instead of a table name, you can pass an ORM model name.model_guard(bool true) - Respect model attributes such as $fillable and $guarded when resolving table columns with a model.filename(string) The path to the CSV file.delimiter(string ,) The CSV field delimiter.offset_rows(int 0) How many rows at the start of the CSV to skip.skip_header_row(bool true) Automatically skip the first row if it's determined to be the header. Settingoffset_rowshigher than 0 bypasses this.mapping(array) Associative array of csvColumnIndex => csvColumnName. See examples for details. If not specified, the first row (after offset) of the CSV will be used as the mapping.aliases(array) Associative array of csvColumnName => aliasColumnName. See examples for details. Allows for flexible CSV column names.hashable(string|array 'password') Hashes the specified field(s) usingbcrypt. Useful if you are importing users and need their passwords hashed. Note: This is EXTREMELY SLOW, large CSVs will take time to import.insert_chunk_size(int 50) An insert callback will trigger everyinsert_chunk_sizerows while reading the CSV.insert_callback(callable) - Override the default insert callback with your own. Callback must accept aCollectionof rows ($chunk).console_logs(bool true) - Show messages in the console. (neglible performance impact)write_logs(bool false) - Write messages to logs. (recommended off for large CSVs)disable_query_log(bool true) - Disable the query log. (recommended on for large CSVs)log_prefix(string) - Customize the log messages
More Examples
CSV with pipe delimited values:
public function __construct()
{
$this->table = 'users';
$this->filename = base_path('database/seeds/csvs/your_csv.csv');
$this->delimiter = '|';
}
Specifying which CSV columns to import:
public function __construct()
{
$this->table = 'users';
$this->filename = base_path('database/seeds/csvs/your_csv.csv');
$this->mapping = [
0 => 'first_name',
1 => 'last_name',
5 => 'age',
];
}
Using a model instead of a table:
public function __construct()
{
$this->model = \App\User::class;
$this->filename = base_path('database/seeds/csvs/your_csv.csv');
// optionally, disable the $model_guard to ignore your model's guarded/fillable attributes
$this->model_guard = false;
}
Skipping the first row of your CSV (Note: If the first row after the offset isn't the header row, a mapping must be defined):
public function __construct()
{
$this->table = 'users';
$this->filename = base_path('database/seeds/csvs/your_csv.csv');
$this->offset_rows = 1;
$this->mapping = [
0 => 'first_name',
1 => 'last_name',
2 => 'password',
];
}
Aliasing a CSV column:
public function __construct()
{
$this->table = 'users';
$this->filename = base_path('database/seeds/csvs/your_csv.csv');
$this->aliases = [
'age' => 'date_of_birth',
];
}
Aliasing a CSV column defined in $mapping:
public function __construct()
{
$this->table = 'users';
$this->filename = base_path('database/seeds/csvs/your_csv.csv');
$this->mapping = [
0 => 'first_name',
1 => 'last_name',
5 => 'birth_date', // in the CSV file, this column is named 'age'
];
$this->aliases = [
'birth_date' => 'date_of_birth',
];
}
Check out the source of Crockett\CsvSeeder\CsvSeeder for more complete information about the available methods.
License
CsvSeeder is open-sourced software licensed under the MIT license
crockett/csv-seeder 适用场景与选型建议
crockett/csv-seeder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 177.84k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2016 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「laravel」 「seeds」 「seeding」 「seed」 「seeder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 crockett/csv-seeder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 crockett/csv-seeder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 crockett/csv-seeder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Create and use JSON files to seed your database in your Laravel applications
Only seed what you haven't seeded yet.
Test data for laravel-excel-seeder
Allows seeding of the database with CSV files and extract clear unit from string
laravel facade to read/write csv file
Collection of tools to use the full power of the Enyalius framework
统计信息
- 总下载量: 177.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-09