lanin/laravel-extend-seeder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lanin/laravel-extend-seeder

Composer 安装命令:

composer require lanin/laravel-extend-seeder

包简介

Extension for Laravel seeder. Seed your database with CSV data.

README 文档

README

Travis

Extend Laravel seeder and populate your DB with CSV data.

Installation

PHP 5.4+ or HHVM 3.3+, Composer and Laravel 5.0+ are required.

To get the latest version of Laravel-Extend-Seeder, simply add the following line to the require block of your composer.json file.

"lanin/laravel-extend-seeder": "dev-master"

You'll then need to run composer install or composer update to download it and have the autoloader updated.

Once it was installed you don't have to register any ServiceProvider, Facade or publish any configs.

All you have to do is to extend your base Seeder class with \Lanin\ExtendSeeder\Seeder and you are good to go!

class Seeder extends \Lanin\ExtendSeeder\Seeder { }

Preparing

Every time you try to seed lots of data, you have to unguard your models. Now you don't have to do it. They will be unguarded automatically in the boot method, that fires right on the start. Also it will disable query log, that extremely slows data import.

Environment protection

If you are afraid to corrupt your production database with seeding it with test data, even if artisan asks you if you want to do it, you can specify protected $environment variable in your seeder. Boot method will check if current environment doesn't match the set one and throw an exception, so you can be sure you don't break your production data.

Seeding

After you extended your base seeder, you will receive two additional methods

seedModel($model)

The best place for this method is the run method of your main DatabaseSeeder.

By default you have to fire call method that will resolve a specified seeder as {$tableName}TableSeeder and launch it, and there resolve your models and do all the stuff.

This method will do it for you. It will find related seeder by model's table and pass your model to it.

Hint: This method returns resolved seeder object, so you can chain your seed methods.

Example:

class Account extends \Illuminate\Database\Eloquent\Model {
	protected $table = 'accounts';
}

class DatabaseSeeder extends Seeder {

	/**
	 * Seed your database.
	 */
	public function run()
	{
		$this->seedModel(\App\Account::class);
	}
}

class AccountsTableSeeder extends Seeder {

	/**
	 * Seed model.
	 */
	public function run()
	{
		$this->getModel()->truncate();
	}
}

seedWithCsv($csvFile = '', $model = null)

This method seeds your database with data from the related csv files.

By default, it tries to find them in your /database/seeds/csv directory.

Files have to be names as {$databaseName}_{$tableName}.csv, but you can always overwrite this behaviour.

Note! If you are using the same seeds to seed mysql and sqlite databases (ie. while testing), you have to define your database name manually, or Seeder will try to find csv files like database.sqlite_your_table.csv. You can do it via \Lanin\ExtendSeeder\Seeder::setDatabaseName('database_name'); method.

If you are calling this method via table seeder, that was called via seedModel, it already knows everything about your model and can resolve related csv file iteslf.

Example:

class AccountsTableSeeder extends Seeder {

	/**
	 * Seed model with CSV.
	 */
	public function run()
	{
		$this->seedWithCsv();
	}
}

Full example you can find in the tests/CsvSeederTest.php

Hacks

You can leave TableSeeders empty and chain csv import via basic seeder.

class DatabaseSeeder extends Seeder {

	/**
	 * Seed your database.
	 */
	public function run()
	{
		$this->seedModel(\App\Account::class)->seedWithCsv();
	}
}

class AccountsTableSeeder extends Seeder {

	/**
	 * Seed model.
	 */
	public function run()
	{

	}
}

You don't even have to create TableSeeders and seed right in your basic seeder.

class DatabaseSeeder extends Seeder {

	/**
	 * Seed your database.
	 */
	public function run()
	{
		$this->setModel(\App\Account::class)->seedWithCsv();
	}
}

If you want to have your csv files to be set in another location you can use static method setCsvPath($csvPath), where you can specify relative from your base_path path.

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

lanin/laravel-extend-seeder 适用场景与选型建议

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

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

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

围绕 lanin/laravel-extend-seeder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

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