Yii2 Seeder

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

https://github.com/sugeng-sulistiyawan/yii2-seeder


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,

        'defaultAction' => 'seed',

        'seederPath' => '@console/seeder',

        'seederNamespace' => 'console\seeder',

        'defaultSeederClass' => 'Seeder',

        'tablesPath' => '@console/seeder/tables',

        'tableSeederNamespace' => 'console\seeder\tables',

        'modelNamespace' => 'common\models',

        'templateSeederFile' => '@diecoding/seeder/views/Seeder.php',

        'templateTableFile' => '@diecoding/seeder/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';

    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_name");
$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: table_name                                                  #
    > # -------------------------------------------------------------------- #
    > #    - 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


class Seeder extends TableSeeder
{
    public function run()
    {
        ModelTableSeeder::create()->run();
    }
}