antonyz89/yii2-seeder
最新稳定版本:1.3.4
Composer 安装命令:
composer require antonyz89/yii2-seeder
包简介
A tool to quickly populate database with fake data
README 文档
README
--
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
namewithout TableSeeder (e.gyii seeder userforUserTableSeeder)
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]
namewithout TableSeeder (e.gyii seeder userforUserTableSeeder)
DatabaseSeeder template:
DatabaseSeeder localization is console\seeder
class DatabaseSeeder extends TableSeeder { const MODEL_COUNT = 10; public function run() { ModelTableSeeder::create()->run(); } }
统计信息
- 总下载量: 26.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-02-05