博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Yii2如何用migrate快速建表
阅读量:4327 次
发布时间:2019-06-06

本文共 2971 字,大约阅读时间需要 9 分钟。

在advanced\console\migrations文件夹下有一个 m130524_201442_init.php 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
use 
yii\db\Schema;
use 
yii\db\Migration;
class 
m130524_201442_init 
extends 
Migration
{
    
public 
function 
up()
    
{
        
$tableOptions 
= null;
        
if 
(
$this
->db->driverName === 
'mysql'
) {
            
// 
            
$tableOptions 
'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'
;
        
}
        
$this
->createTable(
'{
{%user}}'
, [
            
'id' 
=> 
$this
->primaryKey(),
            
'username' 
=> 
$this
->string()->notNull()->unique(),
            
'auth_key' 
=> 
$this
->string(32)->notNull(),
            
'password_hash' 
=> 
$this
->string()->notNull(),
            
'password_reset_token' 
=> 
$this
->string()->unique(),
            
'email' 
=> 
$this
->string()->notNull()->unique(),
            
'status' 
=> 
$this
->smallInteger()->notNull()->defaultValue(10),
            
'created_at' 
=> 
$this
->integer()->notNull(),
            
'updated_at' 
=> 
$this
->integer()->notNull(),
        
], 
$tableOptions
);
    
}
    
public 
function 
down()
    
{
        
$this
->dropTable(
'{
{%user}}'
);
    
}
}

用cmd命令行进入advanced目录 ( 该目录下有yii.bat )

执行命令,选yes(输入y)

1
yii migrate console/migrations/m130524_201442_init.php

 

QQ截图20160730174518.png

 

这样就在数据库中新建了一张表user和另一张表migration

QQ截图20160730175048.png

 

其中migration表的内容大致如下,猜想这个version字段和每次执行命令时php文件的名字&文件里的class名有关,所以每次执行命令时需要改动文件名和文件里面的class名

QQ截图20160730175245.png

 

那么,现在新建一张blog表,包含id、title、content、create_time四个字段

 

  1. 先将该php文件复制备份, 再将该文件重命名为m330524_201442_init.php(随机数字,只要和已有的version不同)

  2. 再将文件内的class m220524_201442_init extends Migration 中的 220524 改为 330524 (和文件名一样)

  3. 编辑字段内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
use 
yii\db\Schema;
use 
yii\db\Migration;
class 
m220524_201442_init 
extends 
Migration
{
    
public 
function 
up()
    
{
        
$tableOptions 
= null;
        
if 
(
$this
->db->driverName === 
'mysql'
) {
          
$tableOptions 
'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="文章表"'
;
        
}
        
$this
->createTable(
'blog'
, [
            
'id' 
=> 
$this
->primaryKey(),
            
'title' 
=> 
$this
->string(100)->notNull()->defaultValue(
''
),
            
'content' 
=> 
$this
->text(),
            
'create_time' 
=> 
$this
->datetime(),
        
],
$tableOptions
);
    
}
    
public 
function 
down()
    
{
        
$this
->dropTable(
'blog'
);
    
}
}

最后执行命令,选yes

1
yii migrate console/migrations/m130524_201442_init.php

可以看到blog表建好了,随机添加两条数据无误

QQ截图20160730180528.png

 

 

 

==========2019.7.23添加============

哎我图呢?......算了

如果要建一张新表

控制台先将路径切换到advanced根目录

yii migrate/create create_blog_table

输入y,回车。会在advanced\console\migrations目录下出现一个新文件m190723_023311_create_blog_table.php,其内容如下

createTable('{
{%blog}}', [ 'id' => $this->primaryKey(), ]); } /** * {@inheritdoc} */ public function safeDown() { $this->dropTable('{
{%blog}}'); }}

目前只有ID自增,现随便加几个字段

public function safeUp()    {        $this->createTable('{
{%blog}}', [ 'id' => $this->primaryKey(), 'title' => $this->string(100)->notNull()->defaultValue(''), 'content' => $this->text(), 'create_time' => $this->datetime(), ]); }

这就准备好了,再在控制台中输入

yii migrate

输入y,回车,则看到mysql里面会增加几张表(migrations目录下有几个文件就新增几张表),其中一张就是blog表

转载于:https://www.cnblogs.com/longzhankunlun/p/6261417.html

你可能感兴趣的文章
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
迷宫实现
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>
学习操作系统导图
查看>>
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
查看>>
SynchronousQueue
查看>>
Python学习笔记-EXCEL操作
查看>>
依照特定轨迹遍历字符串图
查看>>
Mantis 1.1.0 报告问题中设置必填项或取消必填项[Z]
查看>>
爬虫添加代理
查看>>
POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)
查看>>
oracle经常使用函数(2)
查看>>
Iocomp控件之数字显示【图文】
查看>>