roaresearch / yii2-arfixture
Yii2 库,用于运行 ActiveRecord Dao 的 fixtures 并显示进度日志
Requires
- yiisoft/yii2: ~2.0.0
README
库,用于使用 yii\db\ActiveRecord
定义的方法加载数据 fixtures 并显示进度日志。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
composer require --prefer-dist "roaresearch/yii2-arfixture:*"
或者将以下内容添加到您的 composer.json
文件的 require
部分:
"roaresearch/yii2-arfixture": "*"
到 require
部分。
差异
yii\test\Fixture
和 yii\test\ActiveFixture
使用表名和 yii\db\Schema::insert()
方法将数据加载到表中,这意味着模型中定义的所有方法,如验证、事件、行为以及甚至表前缀都将被忽略。
roaresearch\yii2\arfixutre\ARFixture
通过使用 $modelClass
属性创建一个模型来加载数据 fixtures,然后通过使用 ActiveRecord
考虑场景、异常、事件、安全属性、验证错误,并向用户显示详细过程信息。
用法
ARFixture
class UserFixture extends ARFixture { public $modelClass = User::class; }
如果未定义 [[dataFile
]],则将在 data/
子目录中查找与该类相同名称的文件,除去结尾的 Fixture
关键字。例如:UserFixture
将返回文件 data/User.php
或当文件找不到时返回空数组。
数据必须遵循以下结构
return [ // record with no explicit alias, will only show the key number on the log. [ 'username' => 'faryshta', 'name' => 'Angel', 'lastname' => 'Guevara', 'email' => 'angeldelcaos@gmail.com', ], // record with explicit alias, will be shown on the log 'duplicated' => [ 'username' => 'faryshta', 'name' => 'Angel', 'lastname' => 'Guevara', 'email' => 'angeldelcaos@gmail.com', // optional, will apply the scenario before loading the models. 'scenario' => 'api-save', // optional, will check the validation errors. 'attributeErrors' => [ // will check that username has this exact validation error. 'username' => 'Username already in use', // will check that email has any validation error 'email', // the other attributes are not expected to have a validation error. ], ] ];
每一行可以包含以下特殊选项
-
attributeErrors: 以格式为的数组列表的预期验证错误
'attributeErrors' => [ 'attribute1', // will check that it contains any error. 'attribute2' => 'Error Message' // This has to be the error found. ]
警告:如果定义了此选项,即使记录通过所有验证,记录也不会被保存。
-
scenario: 用于模型并处理
Model::load()
和Model::validate()
方法的字符串,如果未定义,则使用$scenarioDefault
文档
待办事项
许可
BSD 许可证 (BSD)。请参阅 许可文件 获取更多信息。