Yii框架数据库迁移

2.数据库迁移之命名规则

在这里只是定义自己的命名规则,PSR规范并没有强调,只是方便自己观看迁移文件路由而已~

  • 创建数据库表:create_tablename_{数据库表名}

  • 数据库表添加字段:add_column_to_{数据库表名}

  • 数据库表添加数据:add_auth_on_{数据库表名}

  • 数据库表删除字段:drop_xxx_from_{数据库表名}

  • 数据库表添加索引:add_index_to_{数据库表名}

  • 添加连接表:create_junction_xxx_and_yyy

 

3.创建数据库表

在控制台输入 php yii migrate/create create_tablename_{数据库表名}

public function safeUp(){        //设置数据表的属性        $tableOptions = null;        if ($this->db->driverName === 'mysql') {            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';        }
//创建一个表 $this->createTable('{{employee_interview}}', [ 'id' => $this->primaryKey(), 'employee_id' => $this->integer(11)->notNull()->comment('员工id'), 'jobnum' => $this->string(11)->notNull()->comment('员工工号'), 'name' => $this->string(11)->notNull()->comment('员工姓名'), 'jobstation' => $this->string(11)->notNull()->comment('员工岗位'), 'resign' => $this->string(11)->notNull()->comment('员工在职情况'), 'entryDate' => $this->string(11)->notNull()->comment('员工入职时间'), 'job_rank' => $this->string(30)->notNull()->comment('员工岗位等级'), 'dept' => $this->string(30)->notNull()->comment('员工部门'), 'is_transfer' => $this->string(50)->notNull()->defaultValue('N')->comment('是否愿意调岗:N/否,(描述)/是'), 'work_record' => $this->string(500)->notNull()->defaultValue('')->comment('工作能力记录'), 'work_level' => $this->string(100)->notNull()->defaultValue('')->comment('工作能力评级'), 'worth_record' => $this->string(500)->notNull()->defaultValue('')->comment('价值观记录'), 'worth_level' => $this->string(100)->notNull()->defaultValue('')->comment('价值观评级'), 'other' => $this->string(255)->notNull()->defaultValue('')->comment('其他'), 'delete_at' => $this->integer(11)->notNull()->defaultValue(0)->comment('删除时间'), ], $tableOptions);
//增加或者修改一个表备注 $this->addCommentOnTable('{{employee_interview}}','员工访谈记录信息表');    }

在这里简单说明一下,

  • $this->string:这里是指当前定义的字段类型

  • notNull:字段类型不能为空

  • defaultValue:字段模型值

  • comment:字段备注

 

4.添加字段

在控制台输入php yii migrate/create add_column_to_{数据库表名}

public function safeUp(){           //addColumn('表名','字段', '属性')      $this->addColumn('negative_review','choose', $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('是否自动分类(1:自动分类 2:否)'));        $this->addColumn('comment_answer','file', $this->string(255)->notNull()->defaultValue('')->comment('数据分析图路径'));    }

 

5.添加索引

在控制台输入 php yii migrate/create add_index_on_{数据库表名}

public function safeUp(){        $this->createIndex('idx_order_num', 'product_evaluation_product', ['order_num']);    }

 

6.添加数据

在控制台输入 php yii migrate/create add_auth_to_{数据库表名}

public function safeUp(){        $data = [            ['Index', 0, '行政管理-列表' , 'N;'],            ['gift-giving', 0, '行政管理-转增' , 'N;']        ];        $column = ['name', 'type', 'description', 'data'];        Yii::$app->db->createCommand()->batchInsert('{{%auth_item}}', $column, $data)->execute();    }

 

7.修改数据

在控制台输入 php yii migrate/create update_auth_to_{数据库表名}

public function safeUp()    {        Yii::$app->db->createCommand()->update('auth_item', ['description' => '访谈问题-回复'] ,['name' => 'ReplyAddInterview'])->execute();        Yii::$app->db->createCommand()->update('auth_item', ['description' => '访谈问题-修改'] ,['name' => 'ReplyEditInterview'])->execute();      }

 

8.删除数据

在控制台输入 php yii migrate/create del_auth_to_{数据库表名}

public function safeUp()    {         Yii::$app->db->createCommand()->delete('auth_item', ['name' => 'AddInterview'])->execute();         Yii::$app->db->createCommand()->delete('auth_item', ['name' => 'EditInterview'])->execute();    }

 

9.提交迁移

./yii migrate   //提交所有迁移,但是已经迁移过的文件不会被执行././yii migrate XXX_xxx_XXX_table     //**指定类名**,提交一个迁移文件

 

10.还原迁移

./yii migrate/down      //还原最近一次迁移./yii migrate 3         //提交前三个可用的迁移./yii migrate/down 3    //还原最近三次迁移

 

11.重做迁移

重做迁移的意思是先还原指定的迁移,然后再次提交:

./yii migrate/redo  //重做最近一次提交的迁移./yii migrate/redo //重做最近三次提交的迁移

 

12.列出迁移

可以通过指令列出提交或者未提交的迁移:

./yii migrate/history  //显示最近10次提交的迁移./yii migrate/history 6 //显示最近5次提交的迁移./yii migrate/history all //显示所有的提交迁移
./yii migrate/new //显示最近10次未提交的迁移./yii migrate/new 6 //显示最近6次未提交的迁移./yii migrate/new all //显示所有的未提交迁移

 

13.其他操作数据库的方法

yii\db\Migration::execute(): 执行一条 SQL 语句yii\db\Migration::insert(): 插入单行数据yii\db\Migration::batchInsert(): 插入多行数据yii\db\Migration::update(): 更新数据yii\db\Migration::delete(): 删除数据yii\db\Migration::createTable(): 创建表yii\db\Migration::renameTable(): 重命名表名yii\db\Migration::dropTable(): 删除一张表yii\db\Migration::truncateTable(): 清空表中的所有数据yii\db\Migration::addColumn(): 加一个字段yii\db\Migration::renameColumn(): 重命名字段名称yii\db\Migration::dropColumn(): 删除一个字段yii\db\Migration::alterColumn(): 修改字段yii\db\Migration::addPrimaryKey(): 添加一个主键yii\db\Migration::dropPrimaryKey(): 删除一个主键yii\db\Migration::addForeignKey(): 添加一个外键yii\db\Migration::dropForeignKey(): 删除一个外键yii\db\Migration::createIndex(): 创建一个索引yii\db\Migration::dropIndex(): 删除一个索引
posted @ 2022-04-24 16:01  技艺追求者  阅读(350)  评论(0)    收藏  举报