germanow / yii2-active-record-seeder
使用测试或默认值填充数据库。
0.0.7
2020-01-21 06:23 UTC
Requires
README
使用测试或默认值填充数据库。
特性
- 使用填充类以不同策略填充表
- 支持关系
安装
运行命令
$ composer require germanow/yii2-active-record-seeder
用法
创建ActiveRecordSeeder,配置填充器,调用fill
方法
use germanow\yii2ActiveRecordSeeder\ActiveRecordSeeder; use germanow\yii2ActiveRecordSeeder\AddFiller; use germanow\yii2ActiveRecordSeeder\OverwriteFiller; $seeder = new ActiveRecordSeeder([ 'fillers' => [ // Default filler is EmptyFiller, which fill table if it's empty [ // Specify name of ActiveRecord model class 'recordClass' => EventType::class, 'data' => [ [ 'id' => 1, 'name' => 'Birthday', ], ], ], // OverwriteFiller delete all records before filling [ 'class' => OverwriteFiller::class, 'recordClass' => EventType::class, 'data' => [ [ 'id' => 1, 'canonicalName' => 'Birthday', 'class' => 'lulz', // Fill relations 'translations' => [ [ 'name' => 'День рождения', 'languageId' => '5', ], ], ], ], ], // AddFiller add records if they not exists in table with such id or attributes. [ 'class' => AddFiller::class, 'recordClass' => EventType::class, 'data' => [ // check exists by id [ 'id' => 1, 'name' => 'Birthday', ], // check exists by canonicalName [ 'name' => 'Meeting', ], ], ], ], ]); $seeder->fill();
原因
我编写此包是为了填充系统表,这些表必须始终被填充,例如EventType
、EventStatus
等。现有解决方案,如yii2-db-seeder,不支持填充策略。