定制 sleeping-owl/seeder 二次开发

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

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

sleeping-owl/seeder

Composer 安装命令:

composer require sleeping-owl/seeder

包简介

Package to create simple seeders with ability to lock/unlock tables

README 文档

README

Latest Stable Version License

Overview

This package will allow you to write simple seeders:

use SleepingOwl\Seeder\DataSeeder;
use SleepingOwl\Seeder\Seeder as SleepingOwlSeeder;

class DatabaseSeeder extends Seeder
{

	public function run()
	{
		 # set global locale (default is en_US)
		SleepingOwlSeeder::setDefaultLocale('de_DE');
		
		 # set global entries count (default is 10)
		SleepingOwlSeeder::setDefaultTimes(10);
		
		 # truncate all tables before seeding (default is off)
		SleepingOwlSeeder::truncateAll();

		# seed Country model
		SleepingOwlSeeder::model(Country::class)
			->seed(function (DataSeeder $schema)
			{
				$schema->title->unique()->country;
			});

		# seed Company model
		SleepingOwlSeeder::model(Company::class)
			->seed(function (DataSeeder $schema)
			{
				$schema->title->unique()->company;
				$schema->address->streetAddress;
				$schema->phone->phoneNumber;
			});

		# seed Contact model
		SleepingOwlSeeder::model(Contact::class)
			->seed(function (DataSeeder $schema)
			{
				$schema->firstName->firstName;
				$schema->lastName->lastName;
				$schema->birthday->dateTimeThisCentury;
				$schema->phone->phoneNumber;
				$schema->address->address;
				$schema->country_id->optional(0.9)->anyOf(Country::class);
				$schema->comment->paragraph(5);
			});

		# seed company_contact table
		SleepingOwlSeeder::table('company_contact')
			->ignoreExceptions()
			->seed(function ($schema)
			{
				$schema->company_id->anyOf(Company::class);
				$schema->contact_id->anyOf(Contact::class);
			});
	}

}

And adds 2 new commands:

  1. php artisan seeder:lock <table> --all — lock table from any changes (table data will be saved and restored after reseeding).

  2. php artisan seeder:unlock <table> --all — unlock table for random seeding.

Installation

  1. Require this packages in your composer.json and run composer update:

    "fzaninotto/faker": "1.5.*@dev",
    "sleeping-owl/seeder": "1.*"
    
  2. After composer update, add service providers to the config/app.php

    'SleepingOwl\Seeder\SeederServiceProvider',
    
  3. That's all.

Seeding

  1. Import classes:

    use SleepingOwl\Seeder\DataSeeder;
    use SleepingOwl\Seeder\Seeder as SleepingOwlSeeder;
  2. Add seeding rule for table or model:

    SleepingOwlSeeder::table('table')->…
    # or
    SleepingOwlSeeder::model(\App\MyModel::class)->
  3. Configure seeding rule (you can configure it globally, see details in "Global Configuration"):

    SleepingOwlSeeder::table('table')
    	->truncate() # delete all data before seeding (default is off)
    	->locale('de_DE') # locale for this seed (default is en_US)
    	->times(100) # entities count to insert (default is 10)
    	->ignoreExceptions() # ignore query exceptions (default is off)
    	->...
  4. Configure seeding schema (see details in "Schema Configuration"):

    SleepingOwlSeeder::table('table')
    	->
    	->seed(function (DataSeeder $schema)
    	{
    		$schema->title->unique()->firstName;
    		$schema->country()->country;
    		$schema->field('my_field')->optional(0.9)->phoneNumber;
    	});
  5. Now you can use default command php artisan db:seed and new seeder:lock/seeder:unlock commands.

Global Configuration

You can configure seeding settings globally:

SleepingOwlSeeder::setDefaultLocale('de_DE');
SleepingOwlSeeder::setDefaultTimes(100);
SleepingOwlSeeder::truncateAll();
SleepingOwlSeeder::setDefaultIgnoreExceptions(true);

This configuration will be used as default for every seeder. Seeder configuration will override global configuration:

SleepingOwlSeeder::setDefaultTimes(100);

SleepingOwlSeeder::table('table')
	->times(5) # this configuration has higher priority
	->...

Schema Configuration

Add Field to the Schema

You must provide schema for seeding. There is 3 ways to add field to the schema:

  1. $schema->field('my_field')
  2. $schema->my_field
  3. $schema->my_field()

All these 3 ways will add field my_field to the schema.

Provide Rules for Seeding the Field

You must use fzaninotto/faker package rules:

 # "phone" will be random phone number
 # "->phone" is field name
 # "->phoneNumber" is seeding rule
$schema->phone->phoneNumber;

 # "my_field" will be unique sentence with 6 words
$schema->my_field->unique()->sentence(6);

 # "country_title" will be country title in 90% cases and null in other 10%
$schema->country_title->optional(0.9)->country;

 # "post_id" will be id of any \App\Post entity
$schema->post_id->anyOf(\App\Post::class);

 # you can use your custom function for getting value for seeding
$schema->my_other_field->call(function ()
{
	return mt_rand(0, 10);
})

 # "int_field" will be random element from "$my_array"
$my_array = [1, 2, 3, 4];
$schema->int_field->randomElement($my_array);

Lock/Unlock Table Seeding

Locking

You can lock table seeding. This command will save table state and restores it with every seeding call.

You can lock one table:

php artisan seeder:lock table_name

Or all tables:

php artisan seeder:lock --all

Unlocking

This command will delete table saved state and restores default behaviour.

php artisan seeder:unlock table_name

or

php artisan seeder:unlock --all

Support Library

You can donate via PayPal or in BTC: 13k36pym383rEmsBSLyWfT3TxCQMN2Lekd

Copyright and License

Package was written by Sleeping Owl for the Laravel framework and is released under the MIT License. See the LICENSE file for details.

sleeping-owl/seeder 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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