用于创建具有锁定/解锁表功能的简单种子包

1.0.2 2015-03-01 11:21 UTC

This package is auto-updated.

Last update: 2024-09-16 20:57:06 UTC


README

Latest Stable Version License

概述

此包允许您编写简单的种子

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);
			});
	}

}

并添加了2个新命令

  1. php artisan seeder:lock <table> --all — 锁定表以防止更改(表数据将在重新播种后保存和恢复)。

  2. php artisan seeder:unlock <table> --all — 解锁表以进行随机播种。

安装

  1. 在您的composer.json中要求此包,并运行composer update

    "fzaninotto/faker": "1.5.*@dev",
    "sleeping-owl/seeder": "1.*"
    
  2. 在composer update之后,将服务提供者添加到config/app.php

    'SleepingOwl\Seeder\SeederServiceProvider',
    
  3. 这就是全部。

播种

  1. 导入类

    use SleepingOwl\Seeder\DataSeeder;
    use SleepingOwl\Seeder\Seeder as SleepingOwlSeeder;
  2. 为表或模型添加播种规则

    SleepingOwlSeeder::table('table')->…
    # or
    SleepingOwlSeeder::model(\App\MyModel::class)->…
  3. 配置播种规则(您可以在全局范围内配置,详见“全局配置”)

    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. 配置播种模式(详见“模式配置”)

    SleepingOwlSeeder::table('table')
    	->…
    	->seed(function (DataSeeder $schema)
    	{
    		$schema->title->unique()->firstName;
    		$schema->country()->country;
    		$schema->field('my_field')->optional(0.9)->phoneNumber;
    	});
  5. 现在您可以使用默认命令php artisan db:seed以及新的seeder:lock/seeder:unlock命令。

全局配置

您可以在全局范围内配置播种设置

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

此配置将用作每个播种器的默认配置。播种器配置将覆盖全局配置

SleepingOwlSeeder::setDefaultTimes(100);

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

模式配置

向模式中添加字段

您必须为播种提供模式。有三种方法可以向模式中添加字段

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

这三种方法都将字段my_field添加到模式中。

为播种字段提供规则

您必须使用fzaninotto/faker包的规则

 # "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);

锁定/解锁表播种

锁定

您可以将表播种锁定。此命令将保存表状态并在每次播种调用中恢复它。

您可以锁定一个表

php artisan seeder:lock table_name

或所有表

php artisan seeder:lock --all

解锁

此命令将删除表保存的状态并恢复默认行为。

php artisan seeder:unlock table_name

php artisan seeder:unlock --all

支持库

您可以通过PayPal或BTC:13k36pym383rEmsBSLyWfT3TxCQMN2Lekd进行捐赠

版权和许可

此包由Sleeping Owl为Laravel框架编写,并使用MIT许可证发布。有关详细信息,请参阅LICENSE文件。