skobka / yii2-migration-generator
从注释生成迁移
v2.0.2
2017-01-04 04:26 UTC
Requires
- doctrine/annotations: v1.2.7
- yiisoft/yii2: ~2.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 19:25:30 UTC
README
从模型或ActiveRecords类(不仅限于)的注释生成迁移。您可以像往常一样编写类,然后请求生成器从代码创建迁移文件。
注意! 仅与yii2控制台兼容。这不是 Gii 扩展
##安装
php composer.phar require --dev skobka/yii2-migration-generator "*"
或者手动将其添加到 composer.json
{
"require-dev": {
"skobka/yii2-migration-generator": "*"
}
}
下一步是启用从 Yii2 控制台生成。在 console/config/main.php 中添加以下代码
use skobka\yii2\migrationGenerator\Controllers\MigrationGeneratorController; return [ //... 'controllerMap' => [ 'migration' => [ 'class' => MigrationGeneratorController::class, ], ], //... ]
##使用
- @Superclass(active=true)
- @Table(name="")
- @Column(name="", type="", typeArgs={}, extra="")
###@Superclass 标识当前类的注释必须包含在子类中。当解析器遍历类时,它将跳过没有 @Superclass 或 @Table 注释的类。解析器将找不到此类父母的注解。如果您想从解析中跳过当前的 @Superclass,只需将选项 active 设置为 false
###@Table 此注解告诉当前类定义了一个表。表名来自属性名
@Table(name="my_first_table")
// You can use @Table() without parameters, then MyModel::tableName() will be used
###@Column 此注解定义了一个表列。您必须指定列的名称和类型。
注意! 列类型必须是 yii\db\SchemaBuilderTrait 可用方法名
- name - 列名称
- type - 列类型
- typeArgs - 类型参数数组,例如:@Column(type="decimal", typeArgs={10,2})
- extra - 将附加到列定义末尾的字符串
完成所有操作后
php yii migration/generate common/models/mymodel
##示例
/** * @Superclass() * @Column(name="id", type="primaryKey") * @Column(name="uid", type="integer", notNull=false) * * @property int id * @property int uid */ class BaseModel { }
<?php use skobka\yii2\migrationGenerator\annotation\Column; use skobka\yii2\migrationGenerator\annotation\Superclass; use skobka\yii2\migrationGenerator\annotation\Table; /** * @Table(name="{{%simple_class}}") * * @Column(name="some", type="string", typeArgs={255}, extra=" DEFAULT '0'") * @Superclass() * use superclass annotation to allow extending class annotation * * @property string some */ class SimpleClass extends BaseModel { }
<?php use skobka\yii2\migrationGenerator\annotation\Column; use skobka\yii2\migrationGenerator\annotation\Table; /** * @Table(name="sub_simple_class") * @Column(name="created", type="integer", notNull=true) * * @property int created */ class SubSimpleClass extends SimpleClass { }