PHPackages                             diecoding/yii2-seeder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. diecoding/yii2-seeder

ActiveLibrary[CLI &amp; Console](/categories/cli)

diecoding/yii2-seeder
=====================

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

v1.1.1(1y ago)411.1k↓45.2%1MITPHPPHP &gt;=7.4.0

Since Apr 4Pushed 1y ago1 watchersCompare

[ Source](https://github.com/wanforge/yii2-seeder)[ Packagist](https://packagist.org/packages/diecoding/yii2-seeder)[ Docs](https://github.com/sugeng-sulistiyawan/yii2-seeder)[ GitHub Sponsors](https://github.com/sponsors/sugeng-sulistiyawan)[ RSS](/packages/diecoding-yii2-seeder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (12)Used By (0)

Yii2 Seeder
===========

[](#yii2-seeder)

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

[![Latest Stable Version](https://camo.githubusercontent.com/ce4aed7e97b37f7b8d6fe5d3aac516f123a818e5b9ed6896584144c6ca8d4911/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646965636f64696e672f796969322d7365656465723f6c6162656c3d737461626c65)](https://packagist.org/packages/diecoding/yii2-seeder)[![Total Downloads](https://camo.githubusercontent.com/e5f8527e2c8d8bdb41bb694e1362368bdb2ef15ce1b11ae8df3420c4b403583f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646965636f64696e672f796969322d736565646572)](https://packagist.org/packages/diecoding/yii2-seeder)[![Latest Stable Release Date](https://camo.githubusercontent.com/d7998e010c3bb43757905a1918bcf4378b1178bae32068d06ade96fd5cb01069/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652d646174652f737567656e672d73756c69737469796177616e2f796969322d736565646572)](https://github.com/sugeng-sulistiyawan/yii2-seeder)[![Quality Score](https://camo.githubusercontent.com/42fe2026df772f2f303065be5ce98ec27685c206d591142faa0468600cefdcdf/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f737567656e672d73756c69737469796177616e2f796969322d736565646572)](https://scrutinizer-ci.com/g/sugeng-sulistiyawan/yii2-seeder)[![Build Status](https://camo.githubusercontent.com/799600d162c2441ba49ddca0c8c5b85cfdf476bfc1cf122125d57a89404b1c22/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f737567656e672d73756c69737469796177616e2f796969322d736565646572)](https://app.travis-ci.com/sugeng-sulistiyawan/yii2-seeder)[![License](https://camo.githubusercontent.com/d1efbbd192bcda9a272cb17e8ad87e3af7a17c35ac5c55612d73f78025ce710d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f737567656e672d73756c69737469796177616e2f796969322d736565646572)](https://github.com/sugeng-sulistiyawan/yii2-seeder)[![PHP Version Require](https://camo.githubusercontent.com/128fca9dc9f74045f1589195a41d43323df4e5c4cf85375e7d139b5be883d461/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f646965636f64696e672f796969322d7365656465722f7068703f636f6c6f723d366637336136)](https://packagist.org/packages/diecoding/yii2-seeder)

Table of Contents
-----------------

[](#table-of-contents)

- [Yii2 Seeder](#yii2-seeder)
    - [Table of Contents](#table-of-contents)
    - [Instalation](#instalation)
    - [Dependencies](#dependencies)
    - [Usage](#usage)
        - [Config](#config)
            - [Basic Config](#basic-config)
            - [Advanced Config](#advanced-config)
        - [Commands](#commands)
        - [Example](#example)
        - [Seeder](#seeder)

Instalation
-----------

[](#instalation)

Package is available on [Packagist](https://packagist.org/packages/diecoding/yii2-seeder), you can install it using [Composer](https://getcomposer.org).

```
composer require diecoding/yii2-seeder "^1.0"
```

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

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

Dependencies
------------

[](#dependencies)

- PHP 7.4+
- [yiisoft/yii2](https://github.com/yiisoft/yii2)
- [yiisoft/yii2-faker](https://github.com/yiisoft/yii2-faker)

Usage
-----

[](#usage)

### Config

[](#config)

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

#### Basic Config

[](#basic-config)

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

#### Advanced Config

[](#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

[](#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

- `name`

    - full path / class name (e.g `yii seeder console\seeder\tables\UserTableSeeder` for `UserTableSeeder`)
    - without TableSeeder (e.g `yii seeder user` for `UserTableSeeder`)
    - with TableSeeder (e.g `yii seeder userTableSeeder` for `UserTableSeeder`)
    - @see  for more usage

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

- `model_name`

    - `yii seeder/create model_name`
    - `yii seeder/create model-name`
    - `yii seeder/create full\path\Class`
    - @see  for handle `[model_name]` options

> 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

[](#example)

```
