diecoding/yii2-seeder

Create fast-fill database with fake data or bulk data for Yii2

v1.1.1 2024-06-04 03:55 UTC

This package is auto-updated.

Last update: 2024-12-04 05:13:10 UTC


README

Create fast-fill database with fake data or bulk data for Yii2.

Latest Stable Version Total Downloads Latest Stable Release Date Quality Score Build Status License PHP Version Require

Table of Contents

Instalation

Package is available on Packagist, you can install it using Composer.

composer require diecoding/yii2-seeder "^1.0"

or add to the require section of your composer.json file.

"diecoding/yii2-seeder": "^1.0"

Dependencies

Usage

Config

Add controllerMap to your config file console/config/main.php or in yii2-basic config/console.php

Basic Config

// ...
'controllerMap' => [
    // ...
    'seeder' => [
        'class' => \diecoding\seeder\SeederController::class,
    ],
    // ...
],
// ...

Advanced Config

// ...
'controllerMap' => [
    // ...
    'seeder' => [
        'class' => \diecoding\seeder\SeederController::class,

        /** @var string the default command action. */
        'defaultAction' => 'seed',

        /** @var string seeder path, support path alias */
        'seederPath' => '@console/seeder',

        /** @var string seeder namespace */
        'seederNamespace' => 'console\seeder',

        /** 
         * @var string this class look like `$this->seederNamespace\Seeder` 
         * default seeder class run if no class selected, 
         * must instance of `\diecoding\seeder\TableSeeder` 
         */
        'defaultSeederClass' => 'Seeder',

        /** @var string tables path, support path alias */
        'tablesPath' => '@console/seeder/tables',

        /** @var string seeder table namespace */
        'tableSeederNamespace' => 'console\seeder\tables',

        /** @var string model namespace */
        'modelNamespace' => 'common\models',

        /** @var string path view template table seeder, support path alias */
        'templateSeederFile' => '@vendor/diecoding/yii2-seeder/src/views/Seeder.php';

        /** @var string path view template seeder, support path alias */
        'templateTableFile' => '@vendor/diecoding/yii2-seeder/src/views/TableSeeder.php';
    ],
    // ...
],
// ...

Commands

yii seeder Seed all tables in Seeder::run() or this seed call $defaultSeederClass::run

yii seeder [name] Seed a table

yii seeder [name]:[funtion_name] Seed a table and run a specific function from selected TableSeeder

yii seeder/create [model_name] Create a TableSeeder in console\seeder\tables\ModelNameTableSeeder

For seeder, if the model is not at the root of the common/models, just add the folder where the model is located inside the common/models directory. You can configure with $modelNamespace config in console\config.

Example:

yii seeder/create entity/user

entity is the folder where User (model) is located inside the common/models directory.

To change the default path for models, just change the $modelNamespace variable in SeederController

Only Seeders within Seeder::run() will be used in yii seeder command

Example

<?php

namespace console\seeder\tables;

use common\models\User;
use common\models\Province;
use diecoding\seeder\TableSeeder;

class UserTableSeeder extends TableSeeder
{
    // public $truncateTable = false;
    // public $locale = 'en_US';

    /**
     * Default execution
     *
     * @return void
     */
    public function run()
    {
        $province = Province::find()->all();

        $count = 100;
        for ($i = 0; $i < $count; $i++) { 
            $this->insert(User::tableName(), [
                'province_id' => $this->faker->randomElement($province)->id,
                'email'       => $this->faker->email,
                'name'        => $this->faker->name,
                'phone'       => $this->faker->phoneNumber,
                'company'     => $this->faker->company,
                'street'      => $this->faker->streetName,
                'zip_code'    => $this->faker->postcode,
                'city'        => $this->faker->city,
                'created_at'  => $this->faker->dateTime(),
                'updated_at'  => $this->faker->dateTime(),
                'status'      => User::STATUS_USER_ACTIVE
            ]);
        }
    }
}

By default, all TableSeeder truncate the table before inserting new data, if you didn't want that to happen in a Seeder, just overwrite $truncateTable:

public $truncateTable = false;

default in TableSeeder:

public $truncateTable = true;

// ...

// truncate table
$this->disableForeignKeyChecks();
$this->truncateTable(/* table names */);
$this->enableForeignKeyChecks();

By default, all TableSeeder faker locale/language get from Yii::$app->language, if you want to use another locale just overwrite $locale:

public $locale = 'en_US';

At the end of every Seeder, if any columns have been forgotten, a message with all the missing columns will appear

    > ########################### MISSING COLUMNS ############################
    > #                                                                      #
    > #    TABLE: {{%user}}                                                  #
    > # -------------------------------------------------------------------- #
    > #    - full_name => varchar(1)                                         #
    > #    - birth_date => date                                              #
    > #    - thumbnail => varchar(64)                                        #
    > # -------------------------------------------------------------------- #
    > #                                                                      #
    > ########################################################################

Seeder

Seeder will be created on first yii seeder if not exist

Here you will put all TableSeeder in ::run()

to run, use yii seeder or yii seeder [name]

Seeder template:

Seeder location in console\seeder

/**
 * Default Seeder
 */
class Seeder extends TableSeeder
{
    /**
     * Default execution
     *
     * @return void
     */
    public function run()
    {
        ModelTableSeeder::create()->run();
    }
}

Read more docs: https://sugengsulistiyawan.my.id/docs/opensource/yii2/seeder/