thegr8dev / iseed
根据现有数据库表数据生成新的Laravel数据库种子文件。
Requires
- php: ^7.4|^8.1
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- illuminate/filesystem: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0
- laravel/framework: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0
- mockery/mockery: ^1.0.0
- phpunit/phpunit: ^8.0
README
逆向种子生成器(iSeed) 是一个Laravel包,提供了一种基于现有数据库表数据生成新种子文件的方法。
安装
1. 使用 Composer 需求
composer require orangehill/iseed
Laravel 5.3.7及以下 或 Laravel 4 需要特定版本
composer require orangehill/iseed:2.2 # Laravel 5.3.7 and below composer require orangehill/iseed:1.1 # Laravel 4
2. 添加服务提供者(Laravel 5.4及以下)
最新版本的Laravel具有自动发现功能,并自动添加服务提供者 - 如果您正在使用5.4.x及以下版本,请记住将其添加到 /app/config/app.php
中的 providers
数组中。
// ... Orangehill\Iseed\IseedServiceProvider::class,
Artisan命令选项
[表名]
必填参数,用于指定用于种子创建的表。使用CSV表示法指定多个表。将为每个表生成种子文件。
示例
php artisan iseed my_table
php artisan iseed my_table,another_table
类名前缀 & 类名后缀
可选参数,用于指定 Seeder 类名和文件名的前缀或后缀。如果您想在已有种子而不覆盖现有种子的情况下为表创建额外的种子,这将很有用。
示例
php artisan iseed my_table --classnameprefix=Customized
输出 CustomizedMyTableSeeder.php
php artisan iseed my_table,another_table --classnameprefix=Customized
输出 CustomizedMyTableSeeder.php 和 CustomizedAnotherTableSeeder.php
php artisan iseed my_table --classnamesuffix=Customizations
输出 MyTableCustomizationsSeeder.php
php artisan iseed my_table,another_table --classnamesuffix=Customizations
输出 MyTableCustomizationsSeeder.php 和 AnotherTableCustomizationsSeeder.php
force
可选参数,用于自动覆盖所需表的任何现有种子。
示例:以下命令将在laravel的种子目录中存在的情况下覆盖 UsersTableSeeder.php
。
php artisan iseed users --force
dumpauto
可选布尔参数,用于控制 composer dump-autoload
命令的执行。默认为true。
示例:以下命令将停止 composer dump-autoload
的执行。
php artisan iseed users --dumpauto=false
clean
可选参数,用于在创建新的种子类之前清理 app/database/seeds/DatabaseSeeder.php
。
示例
php artisan iseed users --clean
database
可选参数,指定DB连接名称。
示例
php artisan iseed users --database=mysql2
max
可选参数,定义从指定表种入的最大条目数。在多个表的情况下,将对所有表应用限制。
示例
php artisan iseed users --max=10
chunksize
可选参数,定义每个插入查询的数据块大小。
示例
php artisan iseed users --chunksize=100
orderby
可选参数,定义用于排序结果所使用的列,当与max参数一起使用时,允许您设置所需的导出数据库条目数。
示例
artisan iseed users --max=10 --orderby=id
direction
可选参数,允许您设置结果排序的方向;与orderby参数一起使用。
示例
artisan iseed users --max=10 --orderby=id --direction=desc
exclude
可选参数,接受逗号分隔的列列表,您希望从正在导出的表中排除这些列。在多个表的情况下,排除将应用于所有表。
示例
php artisan iseed users --exclude=id
php artisan iseed users --exclude=id,created_at,updated_at
prerun
可选参数,将laravel事件名称分配给在播种之前要触发的事件。如果事件监听器返回 false
,种子将自动失败。可以通过传递逗号分隔的DB名称数组为多个表名称分配多个preruns,并相应地传递逗号分隔的prerun事件名称数组。
示例:以下命令将创建一个在播种之前触发名为 'someEvent' 事件的种子文件。
php artisan iseed users --prerun=someEvent
以下示例将 someUserEvent
分配给 users
表种子,将 someGroupEvent
分配给 groups
表种子,并在播种之前执行。
php artisan iseed users,groups --prerun=someUserEvent,someGroupEvent
以下示例仅将一个someGroupEvent
分配给在种子之前执行的groups
表种子,用户表prerun的值在此处省略,因此users
表种子将不会分配任何prerun事件。
php artisan iseed users,groups --prerun=,someGroupEvent
postrun
可选参数,将种子完成后要触发的laravel事件名称分配给。如果事件监听器返回false
,种子将执行,但会抛出一个postrun失败异常。您可以通过传递以逗号分隔的数据库名称数组,并相应地传递以逗号分隔的postrun事件名称数组,为多个表名分配多个postrun。
示例:以下命令将生成一个种子文件,在种子完成后将触发名为'someEvent'的事件。
php artisan iseed users --postrun=someEvent
以下示例将someUserEvent
分配给users
表种子,将someGroupEvent
分配给groups
表种子,以在种子之后执行。
php artisan iseed users,groups --postrun=someUserEvent,someGroupEvent
以下示例仅将一个someGroupEvent
分配给在种子之后执行的groups
表种子,用户表postrun的值在此处省略,因此users
表种子将不会分配任何postrun事件。
php artisan iseed users,groups --postrun=,someGroupEvent
noindex
使用--noindex,可以将种子生成为一个非索引数组。此功能的用例是在需要合并两个种子文件时。
示例
php artisan iseed users --noindex
用法
要为您的用户表生成种子文件,只需调用:\Iseed::generateSeed('users', 'connectionName', 'numOfRows');
。 connectionName
和numOfRows
不是必需的参数。
这将创建一个位于/database/seeds
(对于Laravel 4为/app/database/seeds
)的文件,其内容与以下示例类似
<?php // File: /database/seeds/UsersTableSeeder.php class UsersTableSeeder extends Seeder { /** * Auto generated seed file * * @return void */ public function run() { \DB::table('users')->truncate(); \DB::table('users')->insert(array ( 0 => array ( 'id' => '1', 'email' => 'admin@admin.com', 'password' => '$2y$10$tUGCkQf/0NY3w1l9sobGsudt6UngnoVXx/lUoh9ElcSOD0ERRkK9C', 'permissions' => NULL, 'activated' => '1', 'activation_code' => NULL, 'activated_at' => NULL, 'last_login' => NULL, 'persist_code' => NULL, 'reset_password_code' => NULL, 'first_name' => NULL, 'last_name' => NULL, 'created_at' => '2013-06-11 07:47:40', 'updated_at' => '2013-06-11 07:47:40', ), 1 => array ( 'id' => '2', 'email' => 'user@user.com', 'password' => '$2y$10$ImNvsMzK/BOgNSYgpjs/3OjMKMHeA9BH/hjl43EiuBuLkZGPMuZ2W', 'permissions' => NULL, 'activated' => '1', 'activation_code' => NULL, 'activated_at' => NULL, 'last_login' => '2013-06-11 07:54:57', 'persist_code' => '$2y$10$C0la8WuyqC6AU2TpUwj0I.E3Mrva8A3tuVFWxXN5u7jswRKzsYYHK', 'reset_password_code' => NULL, 'first_name' => NULL, 'last_name' => NULL, 'created_at' => '2013-06-11 07:47:40', 'updated_at' => '2013-06-11 07:54:57', ), )); } }
此命令还将更新/database/seeds/DatabaseSeeder.php
(对于Laravel 4为/app/database/seeds/DatabaseSeeder.php
)以包括对新生成的种子类的调用。
如果您希望定义一个自定义iSeed模板,其中将放置所有调用,您可以通过在/database/seeds/DatabaseSeeder.php
(对于Laravel 4为/app/database/seeds/DatabaseSeeder.php
)中的任何位置使用#iseed_start
和#iseed_end
模板来完成此操作,例如
<?php // File: /database/seeds/DatabaseSeeder.php class DatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Eloquent::unguard(); if(App::environment() == "local") { throw new \Exception('Only run this from production'); } #iseed_start // here all the calls for newly generated seeds will be stored. #iseed_end } }
或者,您可以从命令行使用Artisan运行Iseed,例如:php artisan iseed users
。对于生成多个种子文件,应在命令中作为参数发送以逗号分隔的表名列表,例如:php artisan iseed users,posts,groups
。
如果您尝试生成一个已存在的种子文件,命令将询问您是否要覆盖它。如果您希望默认覆盖它,请使用Artisan命令选项--force
,例如:php artisan iseed users --force
。
如果您希望清除iSeed模板,可以使用Artisan命令选项--clean
,例如:php artisan iseed users --clean
。这将清除模板,在创建新的种子类之前从app/database/seeds/DatabaseSeeder.php
中清除。
您可以使用Artisan命令选项--database=connection_name
指定用于创建新种子文件的数据库连接,例如:php artisan iseed users --database=mysql2
。
要限制从表导出的行数,请使用Artisan命令选项--max=number_of_rows
,例如:php artisan iseed users --max=10
。如果您在导出多个指定的表时使用此选项,则将对它们应用此限制。
要(重新)种子数据库,请转到终端并运行Laravel的db:seed命令
(php artisan db:seed
)。
请注意,一些用户在导出大型数据库表时遇到了问题(从具有许多记录的表中种子的错误)。该问题已通过将输入数据分成较小的元素插入语句块来解决。由于您可能需要在具有大量列的数据库表的一些极端情况下更改块大小值,因此块大小在iSeed的config.php
文件中可配置。
'chunk_size' => 500 // Maximum number of rows per insert statement