mootensai/yii2-seeder

A tool to quickly populate database with fake data

Installs: 300

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 3

Open Issues: 0

Type:yii2-extension

1.3.3 2023-05-05 11:38 UTC

This package is auto-updated.

Last update: 2024-11-05 12:13:29 UTC


README

Donate with PayPal

--

Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist mootensai/yii2-seeder dev-master

or add

"mootensai/yii2-seeder": "dev-master"

to the require section of your composer.json file.

USAGE

console/config/main.php

'controllerMap' => [
    'seeder' => [
        'class' => 'mootensai\seeder\SeederController'
    ],
],

SEEDER COMMANDS

yii seeder Seed all tables in Database::run()

yii seeder [name] Seed a table

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

  • name without TableSeeder (e.g yii seeder user for UserTableSeeder)

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

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.

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 DatabaseSeeder::run() will be used in yii seeder command

SEEDER

EXAMPLE TEMPLATE

<?php

namespace console\seeder\tables;

use common\models\user\User;
use console\seeder\DatabaseSeeder;
use mootensai\seeder\TableSeeder;
use Yii;

class UserTableSeeder extends TableSeeder
{
    function run()
    {
        loop(function ($i) {
            $this->insert(User::tableName(), [
                'email' => "user$i@gmail.com",
                'name' => $this->faker->name,
                'document' => $this->faker->numerify('##############'),
                'street' => $this->faker->streetName,
                'number' => $this->faker->numerify('###'),
                'zip_code' => $this->faker->postcode,
                'city' => $this->faker->city,
                'status' => User::STATUS_USER_ACTIVE
                //created_at and updated_at are automatically added
            ]);
        }, DatabaseSeeder::USER_COUNT);
    }
}

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

public $skipTruncateTables = true;

default in TableSeeder:

public $skipTruncateTables = false;

...

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

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}}                                     #
    > #    name => varchar(255)                              #
    > #    age => int(2)                                     #
    > ########################################################

DatabaseSeeder

DatabaseSeeder will be created on first yii seeder/create model

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

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

  • name without TableSeeder (e.g yii seeder user for UserTableSeeder)

DatabaseSeeder template:

DatabaseSeeder localization is console\seeder

class DatabaseSeeder extends TableSeeder
{

    const MODEL_COUNT = 10;

    public function run()
    {
        ModelTableSeeder::create()->run();
    }

}