在thinkphp5中提供了數據遷移工具(think-migration),它是機遇phinx開發(文檔地址:http://docs.phinx.org/en/latest/)
一:配置think-migration
在commond.php 中添加
<?php return [ "think\\migration\\command\\migrate\\Create", "think\\migration\\command\\migrate\\Run", "think\\migration\\command\\migrate\\Rollback", "think\\migration\\command\\migrate\\Status", "think\\migration\\command\\seed\\Create", "think\\migration\\command\\seed\\Run", ]; ?>
注意由於think-migration存放在thinkphp/vendor中 所以在think中需要將vendor加入auoload
require __DIR__.'/../thinkphp/vendor/autoload.php';
二:命令行運行
在命令行輸入php think 可以看見
migrate:數據庫遷移工具 seed:數據庫填充工具
主要討論migrate:
migrate:create : 創建一個新的數據遷移類,php think migrate:create <file>,文件名須采用駝峰命名法
forexample:php think migrate:create ScProductImage 文件會在制定目錄下生成一個php文件
***********************************************************
migrate:run : 完成數據遷移工作 php think migrate:run
***********************************************************
migrate:status:查看migrate工作狀態 php think migrate:status
***********************************************************************************************
migrate:rollback : 回滾數據到指定的一個數據遷移版本 php think migrate:rollback -t <timeline>
<timeline> 就是我們上圖上面紅框表示的值
三:migrate文件編寫
在migrate中有三個方法
up:在migrate:run時執行(前提是文件中不存在change方法)
down:在migrate:rollback時執行(前提是文件中不存在change方法)
change:migrate:run 和migrate:rollback時執行 (如果存在該方法 則不會去執行up 與down)