masitings/seedmaker

基于现有数据库表数据生成新的 Laravel 数据库种子文件。

1.0 2023-03-17 06:30 UTC

This package is auto-updated.

Last update: 2024-09-26 20:33:05 UTC


README

这是一个 Laravel 扩展包,提供了一种基于现有数据库表数据生成新种子文件的方法。

安装

1. 使用 Composer 需要

composer require masitings/seedmaker

Artisan 命令选项

[表名]

必选参数,定义了哪些表将被用于种子创建。使用 CSV 表示法指定多个表。将为每个表生成种子文件。

示例

php artisan seedmaker my_table
php artisan seedmaker my_table,another_table

类名前缀 & 类名后缀

可选参数,指定 Seeder 类名和文件名的前缀或后缀。如果您想为已有种子的表创建额外的种子而不覆盖现有种子,这将很有用。

示例

php artisan seedmaker my_table --classnameprefix=Customized

输出 CustomizedMyTableSeeder.php

php artisan seedmaker my_table,another_table --classnameprefix=Customized

输出 CustomizedMyTableSeeder.php 和 CustomizedAnotherTableSeeder.php

php artisan seedmaker my_table --classnamesuffix=Customizations

输出 MyTableCustomizationsSeeder.php

php artisan seedmaker my_table,another_table --classnamesuffix=Customizations

输出 MyTableCustomizationsSeeder.php 和 AnotherTableCustomizationsSeeder.php

force

可选参数,用于自动覆盖所需表中的任何现有种子

示例:以下命令将覆盖 laravel 的种子目录中已存在的 UsersTableSeeder.php

php artisan seedmaker users --force

dumpauto

可选布尔参数,控制 composer dump-autoload 命令的执行。默认为 true。

示例:以下示例将停止 composer dump-autoload 的执行

php artisan seedmaker users --dumpauto=false

clean

可选参数,将在创建新种子类之前清理 app/database/seeds/DatabaseSeeder.php

示例

php artisan seedmaker users --clean

database

可选参数,指定 DB 连接名称。

示例

php artisan seedmaker users --database=mysql2

max

可选参数,定义从指定表中种入的最大条目数。对于多个表,限制将应用于所有表。

示例

php artisan seedmaker users --max=10

chunksize

可选参数,定义每个插入查询的数据块大小。

示例

php artisan seedmaker users --chunksize=100

orderby

可选参数,定义用于排序结果的列,当与允许设置所需导出数据库条目数量的 max 参数结合使用时。

示例

artisan seedmaker users --max=10 --orderby=id

direction

可选参数,允许您设置排序结果的排序方向;与orderby参数一起使用。

示例

artisan seedmaker users --max=10 --orderby=id --direction=desc

exclude

可选参数,接受以逗号分隔的列列表,您希望从导出的表中排除。对于多个表,排除将应用于所有表。

示例

php artisan seedmaker users --exclude=id
php artisan seedmaker users --exclude=id,created_at,updated_at

prerun

可选参数,将 Laravel 事件名称分配给在种子之前触发的事件。如果事件监听器返回 false,种子将自动失败。可以通过传递以逗号分隔的数据库名称数组,以及相应地传递以逗号分隔的事件名称数组,为多个表名分配多个 preruns。

示例:以下命令将创建一个种子文件,在种子之前触发名为 'someEvent' 的事件。

php artisan seedmaker users --prerun=someEvent

以下示例将 someUserEvent 分配给 users 表种子,并将 someGroupEvent 分配给 groups 表种子,在种子之前执行。

php artisan seedmaker users,groups --prerun=someUserEvent,someGroupEvent

以下示例仅将 someGroupEvent 分配给 groups 表种子,在种子之前执行。此处省略了 users 表的 prerun 值,因此 users 表种子将没有分配的 prerun 事件。

php artisan seedmaker users,groups --prerun=,someGroupEvent

postrun

可选参数,用于在播种完成后触发一个Laravel事件名称。如果事件监听器返回false,则播种将执行,但会抛出一个postrun失败的异常。您可以通过传递以逗号分隔的数据库名称数组以及相应地传递以逗号分隔的事件名称数组,为多个表名分配多个postrun。

示例:以下命令将创建一个播种文件,播种完成后将触发名为'someEvent'的事件。

php artisan seedmaker users --postrun=someEvent

以下示例将someUserEvent分配给users表的播种,将someGroupEvent分配给groups表的播种,并在播种后执行。

php artisan seedmaker users,groups --postrun=someUserEvent,someGroupEvent

以下示例仅将someGroupEvent分配给groups表的播种,在播种后执行。这里省略了users表postrun的值,因此users表的播种将没有分配postrun事件。

php artisan seedmaker users,groups --postrun=,someGroupEvent

noindex

使用--noindex,可以将播种生成为一个非索引数组。此功能的用例是在需要合并两个播种文件时。

示例

php artisan seedmaker users --noindex

用法

要为您的用户表生成一个播种文件,只需调用:\seedmaker::generateSeed('users', 'connectionName', 'numOfRows');connectionNamenumOfRows不是必需的参数。

这将创建一个位于/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)以包含对新生成的播种类的调用。

如果您希望,可以定义自定义seedmaker模板,其中所有调用都将放置在其中。您可以通过在/database/seeds/DatabaseSeeder.php(对于Laravel 4为/app/database/seeds/DatabaseSeeder.php)中的任何位置使用#seedmaker_start#seedmaker_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');
        }

        #seedmaker_start

        // here all the calls for newly generated seeds will be stored.

        #seedmaker_end
    }

}

或者,您可以从命令行使用Artisan运行seedmaker,例如:php artisan seedmaker users。对于生成多个播种文件,应以逗号分隔的表名列表作为命令的参数发送,例如:php artisan seedmaker users,posts,groups

如果您尝试生成已存在的播种文件,命令将询问您是否要覆盖它。如果您希望默认覆盖它,请使用Artisan命令选项--force,例如:php artisan seedmaker users --force

如果您希望清除seedmaker模板,可以使用Artisan命令选项--clean,例如:php artisan seedmaker users --clean。这将在新创建播种类之前清除模板从app/database/seeds/DatabaseSeeder.php

您可以使用Artisan命令选项--database=connection_name指定用于创建新播种文件的数据库连接,例如:php artisan seedmaker users --database=mysql2

要限制从表中导出的行数,请使用Artisan命令选项--max=number_of_rows,例如:php artisan seedmaker users --max=10。如果您在导出多个指定的表时使用此选项,则将应用限制到所有这些表中。

要(重新)播种数据库,请转到终端并运行Laravel的db:seed commandphp artisan db:seed)。

请注意,一些用户遇到了大型数据库表导出的问题(从具有许多记录的表中播种时的错误)。问题通过将输入数据分成每个插入语句的小元素块来解决。由于您可能需要在某些极端情况下更改块大小值,其中数据库表具有大量列,因此块大小可以在seedmaker的config.php文件中配置。

'chunk_size' => 500 // Maximum number of rows per insert statement