antonyz89/yii2-seeder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

antonyz89/yii2-seeder

最新稳定版本:1.3.4

Composer 安装命令:

composer require antonyz89/yii2-seeder

包简介

A tool to quickly populate database with fake data

README 文档

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 antonyz89/yii2-seeder dev-master 

or add

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

to the require section of your composer.json file.

USAGE

console/config/main.php

'controllerMap' => [ 'seeder' => [ 'class' => 'antonyz89\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 antonyz89\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(); } }

统计信息

  • 总下载量: 26.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固