et-soft/yii2-migrations-create

Yii2 的控制台脚本,用于从现有数据库创建迁移文件。

v0.1.2 2016-07-29 13:59 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:08 UTC


README

Latest Stable Version License Total Downloads Monthly Downloads Daily Downloads

Yii2 的控制台脚本,用于从现有数据库创建 Yii2 迁移文件。

安装

运行以下命令之一:

$ php composer.phar require et-soft/yii2-migrations-create "*"

或者在您的 composer.json 文件的 require 部分添加以下内容:

"et-soft/yii2-migrations-create": "*"

to the require section of your composer.json file.

在 config/console.php 中添加以下部分:

'controllerMap' => [
    'migrations' => 'etsoft\yii2migrations\MigrationsController',
]

使用

在控制台中,输入以下命令:

./yii migrations

在 app\migrations 目录中为数据库中的所有表创建迁移文件。

您还可以在控制台中输入以下命令:

./yii migrations/table table_name

为 app\migrations 目录中选定的数据库表创建迁移文件。

示例

对于具有模式的现有 MySQL 表

 CREATE TABLE test
 (
     id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
     user_id INT(11) COMMENT 'ID user',
     name VARCHAR(255) NOT NULL COMMENT 'Name',
     phone VARCHAR(50) NOT NULL COMMENT 'Phone',
     created DATETIME NOT NULL COMMENT 'Date created'
 );

在 app\migrations 目录中创建迁移脚本,文件名为 m_test.php,内容如下:

<?php

use yii\db\Migration;

/**
 * Handles the creation for table `{{%test}}`.
 */
class m_test extends Migration
{
    /**
     * @inheritdoc
     */
    public function up()
    {
        $this->createTable('{{%test}}', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()->comment('ID user'),
            'name' => $this->string(255)->notNull()->comment('Name'),
            'phone' => $this->string(50)->notNull()->comment('Phone'),
            'created' => $this->dateTime()->notNull()->comment('Date created'),
        ]);
    }

    /**
     * @inheritdoc
     */
    public function down()
    {
        $this->dropTable('{{%test}}');
    }
}

下一版本的功能

  • 支持外键
  • 支持 ENUM 字段