首页 > 开发 > Php > 正文

YII Framework框架使用YIIC快速创建YII应用之migrate用法实例详解

2020-02-21 20:46:37
字体:
来源:转载
供稿:网友

本文实例讲述了YII Framework框架使用YIIC快速创建YII应用之migrate用法。分享给大家供大家参考,具体如下:

yii migrate

查看帮助

/*/www/yii_dev/yii/framework# php yiic migrate helpError: Unknown action: helpUSAGE yiic migrate [action] [parameter]DESCRIPTION This command provides support for database migrations. The optional 'action' parameter specifies which specific migration task to perform. It can take these values: up, down, to, create, history, new, mark. If the 'action' parameter is not given, it defaults to 'up'. Each action takes different parameters. Their usage can be found in the following examples.EXAMPLES* yiic migrateApplies ALL new migrations. This is equivalent to 'yiic migrate to'.* yiic migrate create create_user_tableCreates a new migration named 'create_user_table'.* yiic migrate up 3Applies the next 3 new migrations.* yiic migrate downReverts the last applied migration.* yiic migrate down 3Reverts the last 3 applied migrations.* yiic migrate to 101129_185401Migrates up or down to version 101129_185401.* yiic migrate mark 101129_185401Modifies the migration history up or down to version 101129_185401.No actual migration will be performed.* yiic migrate historyShows all previously applied migration information.* yiic migrate history 10Shows the last 10 applied migrations.* yiic migrate newShows all new migrations.* yiic migrate new 10Shows the next 10 migrations that have not been applied.*/

在我们开发程序的过程中,数据库的结构也是不断调整的。我们的开发中要保证代码和数据库库的同步。因为我们的应用离不开数据库。例如: 在开发过程中,我们经常需要增加一个新的表,或者我们后期投入运营的产品,可能需要为某一列添加索引。我们必须保持数据结构和代码的一致性。如果代码和数据库不同步,可能整个系统将无法正常运行。出于这个原因。yii提供了一个数据库迁移工具,可以保持代码和数据库是同步。方便数据库的回滚和更新。

功能正如描述。主要提供了数据库迁移功能。

命令格式

yiic migrate [action] [parameter]

action参数用来制定执行哪一个迁移任务。可以一使用

up, down, to, create, history, new, mark.这些命令

如果没有action参数,默认为up

parameter根据action的不同而有所变化。

上述例子中给出了说明。

官方也给出了详细的例子。

http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.migration#creating-migrations

这里不再详细累述。用到的时候参考使用就可以了。

补充:yii2.0使用migrate创建后台登陆

重新创建一张数据表来完成后台登陆验证

为了大家看得明白,直接贴代码

一、使用Migration创建表admin

console/migrations/m130524_201442_init.php

use yii/db/Schema;use yii/db/Migration;class m130524_201442_init extends Migration{  const TBL_NAME = '{{%admin}}';  public function safeUp()  {    $tableOptions = null;    if ($this->db->driverName === 'mysql') {      // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci      $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';    }    $this->createTable(self::TBL_NAME, [      'id' => Schema::TYPE_PK,      'username' => Schema::TYPE_STRING . ' NOT NULL',      'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL',      'password_hash' => Schema::TYPE_STRING . ' NOT NULL', //密码      'password_reset_token' => Schema::TYPE_STRING,      'email' => Schema::TYPE_STRING . ' NOT NULL',      'role' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',      'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',      'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',      'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',    ], $tableOptions);    $this->createIndex('username', self::TBL_NAME, ['username'],true);    $this->createIndex('email', self::TBL_NAME, ['email'],true);  }  public function safeDown()  {    $this->dropTable(self::TBL_NAME);  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表