nkovacs / yii2-table-builder
为 Yii 2 的表格构建迁移助手扩展
1.0.3
2016-09-21 14:08 UTC
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-08-25 02:54:02 UTC
README
为 Yii 2 的表格构建迁移助手扩展
安装
安装此扩展的首选方式是通过 Composer。
运行以下命令之一:
php composer.phar require --prefer-dist nkovacs/yii2-table-builder "*"
或者将以下内容添加到你的 composer.json
文件的 require 部分:
"nkovacs/yii2-table-builder": "*"
to the require section of your composer.json
file.
使用方法
为了使用此扩展,你的迁移必须继承自 \nkovacs\tablebuilder\Migration
。一旦扩展安装完成,它将在应用程序启动过程中覆盖 migrate 命令的 templateFile 属性,允许你在新创建的迁移中使用扩展的 Migration 类,前提是你没有自己覆盖 templateFile。
一个示例迁移
<?php
use yii\db\Schema;
class m140701_113939_test extends \nkovacs\tablebuilder\Migration
{
public $tables;
public function init()
{
parent::init();
$this->tables = [
'{{%table_a}}' => [
'id' => Schema::TYPE_PK,
'name' => Schema::TYPE_STRING,
],
'{{%table_b}}' => [
'id' => Schema::TYPE_PK,
'name' => Schema::TYPE_STRING,
],
'{{%table_c}}' => [
'id' => Schema::TYPE_PK,
'user_id' => [Schema::TYPE_INTEGER, '{{%user}}', 'id', 'delete' => 'SET NULL', 'update' => 'CASCADE'],
'a_id' => [Schema::TYPE_INTEGER, '{{%table_a}}', 'id', 'delete' => 'RESTRICT', 'update' => 'CASCADE'],
'b_id' => [Schema::TYPE_INTEGER, '{{%table_b}}', 'id', 'delete' => 'RESTRICT', 'update' => 'CASCADE'],
'name' => Schema::TYPE_STRING,
],
'{{%table_d}}' => [
'a_id' => [Schema::TYPE_INTEGER, '{{%table_a}}', 'id', 'delete' => 'RESTRICT', 'update' => 'CASCADE'],
'b_id' => [Schema::TYPE_INTEGER, '{{%table_b}}', 'id', 'delete' => 'RESTRICT', 'update' => 'CASCADE'],
['primary' => ['a_id', 'b_id']],
]
];
}
public function up()
{
$this->build($this->tables);
}
public function down()
{
$this->teardown($this->tables);
}
}