codesvault/wp-seeder

WordPress 数据库种子工具

1.1.0 2023-06-16 21:01 UTC

This package is auto-updated.

Last update: 2024-09-16 23:54:09 UTC


README

在终端中从数据库添加示例数据。

安装

需要使用 composer 进行安装:composer install codesvault/wp-seeder
支持 PHP >= 7.1




使用方法

要创建一个新的种子,运行 ./vendor/bin/wpseed new 并提供必要的输入。
它将在 /database 目录中生成 /seeders 目录。不要将此目录移动到其他位置,它必须在这里。在 /database/seeders 文件夹中,您将找到自动生成的种子,文件名将与您输入的类名相同。


现在根据您要存储数据的表名更改 $table 属性。 $row 属性用于指定要生成的表中的行数。为了生成示例数据,WP Seeder 内置了对 FakerPHP 的支持。

class yourClassName extends WPSeeder
{
    public $table = "cv_users";    // db table name without prefix, default is posts.
    public $row = 5000;      // number of db table row will create, default is 1.

    public function run()
    {
        // add data that need to be inserted in database.
        // array key is the column name, value is data that will be stored.
        return array(
            'name' => $this->faker()->unique()->name(),
            'email' => $this->faker()->unique()->email(),
            'password' => $this->faker()->unique()->password()
        );
    }
}

如果您想为不同的表创建更多种子,只需重复上述过程。
现在只需在终端中运行 ./vendor/bin/wpseed store,数据将被存储在数据库中。


.