yii2数据迁移migrate

参考:https://blog.csdn.net/gao_yu_long/article/details/51742082
首先,到有yii.bat的根目录执行命令(注意要是dos命令框)

.\yii migrate/create paper

随后在console/migrations/m180902_080020_paper.php这样个文件,这是我来生成表的类,然后在改变下他继承的Migration,再改写这个方法

  public function up()
    {
        $this->createTable('paper', array_merge([
            'id' => $this->primaryKey(),
            'title' => $this->string()->notNull()->comment('标题'),
            'url' => $this->text()->notNull()->comment('图片地址'),
            'type' => $this->integer()->notNull()->defaultValue(0)->comment('大类型'),
            'kind' => $this->integer()->notNull()->defaultValue(0)->comment('中类型'),
            'introduction' => $this->string()->comment('简介'),
            'praise' => $this->integer()->comment('点赞数'),
            'view' => $this->integer()->comment('浏览量'),
        ], $this->commonColumns([
            'status', 'created_at', 'created_by', 'updated_at', 'updated_by'
        ])// 注意这边的分隔
        ), $this->setTableComment('壁纸主表'));

    }
    /*
    // 删除表 的一条和一整张表(注释这个)
       public function down()
       {
           echo "m180902_080020_paper cannot be reverted.\n";

           return false;
       }
       *

然后在执行命令

.\yii migrate m180902_080020_paper

这样表就生成了,同时在表migration记录了,如果想再执行一次得删掉这个记录。

posted @ 2018-09-02 16:57  蜗牛使劲冲  阅读(18)  评论(0)    收藏  举报  来源