thinkphp之migration 迁移文件的使用
創(chuàng)建遷移類,首字母必須為大寫
php think migrate:create Users
可以看到目錄下有新文件 .databasemigrations20161117144043_users.php
使用實(shí)例
<?php
use PhinxMigrationAbstractMigration;
class Users extends AbstractMigration
{
/**
* Change Method.
*/
public function change()
{
// create the table
$table = $this->table('users',array('engine'=>'MyISAM'));
$table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用戶名,登陸使用'))
->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用戶密碼'))
->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陸狀態(tài)'))
->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陸標(biāo)識(shí)'))
->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登錄IP'))
->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登錄時(shí)間'))
->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'刪除狀態(tài),1已刪除'))
->addIndex(array('username'), array('unique' => true))
->create();
}
/**
* Migrate Up.
*/
public function up()
{
}
/**
* Migrate Down.
*/
public function down()
{
}
}
以上為thinkphp手冊(cè)提供的 介紹
======================================================分割線============================================================================
1.創(chuàng)建數(shù)據(jù)表
使用 up 和 down方法
Up 方法
up方法會(huì)在Phinx執(zhí)行遷移命令時(shí)自動(dòng)執(zhí)行,并且該腳本之前并沒(méi)有執(zhí)行過(guò)。你應(yīng)該將修改數(shù)據(jù)庫(kù)的方法寫在這個(gè)方法里。
Down 方法
down方法會(huì)在Phinx執(zhí)行回滾命令時(shí)自動(dòng)執(zhí)行,并且該腳本之前已經(jīng)執(zhí)行過(guò)遷移。你應(yīng)該將回滾代碼放在這個(gè)方法里。
Save 方法
當(dāng)操作 Table 對(duì)象時(shí),Phinx 提供了一些操作來(lái)改變數(shù)據(jù)庫(kù)。如果你不清楚該使用什么操作,建議你使用 save 方法。它將自動(dòng)識(shí)別插入或者更新操作,并將改變應(yīng)用到數(shù)據(jù)庫(kù)。
創(chuàng)建數(shù)據(jù)表時(shí)推薦使用 這三個(gè)方法!
字段操作
字段類型
字段類型如下:
biginteger
binary
boolean
date
datetime
decimal
float
integer
string
text
time
timestamp
uuid
另外,MySQL adapter 支持enum、set、blob和json(json需要 MySQL 5.7 或者更高)
Postgres adapter 支持smallint、json、jsonb和uuid(需要 PostgresSQL 9.3 或者更高)
2 字段選項(xiàng)
以下是有效的字段選項(xiàng):
所有字段:
| 選項(xiàng) | 描述 |
|---|---|
| limit | 為string設(shè)置最大長(zhǎng)度 |
| length | limit 的別名 |
| default | 設(shè)置默認(rèn)值 |
| null | 允許空 |
| after | 指定字段放置在哪個(gè)字段后面 |
| comment | 字段注釋 |
decimal類型字段:
| 選項(xiàng) | 描述 |
|---|---|
| precision | 和 scale 組合設(shè)置精度 |
| scale | 和 precision 組合設(shè)置精度 |
| signed | 開(kāi)啟或關(guān)閉 unsigned 選項(xiàng)(僅適用于 MySQL) |
enum和set類型字段:
| 選項(xiàng) | 描述 |
|---|---|
| values | 用逗號(hào)分隔代表值 |
integer和biginteger類型字段:
| 選項(xiàng) | 描述 |
|---|---|
| identity | 開(kāi)啟或關(guān)閉自增長(zhǎng) |
| signed | 開(kāi)啟或關(guān)閉 unsigned 選項(xiàng)(僅適用于 MySQL) |
timestamp類型字段:
| 選項(xiàng) | 描述 |
|---|---|
| default | 設(shè)置默認(rèn)值 (CURRENT_TIMESTAMP) |
| update | 當(dāng)數(shù)據(jù)更新時(shí)的觸發(fā)動(dòng)作 (CURRENT_TIMESTAMP) |
| timezone | 開(kāi)啟或關(guān)閉 with time zone 選項(xiàng) |
可以在標(biāo)準(zhǔn)使用addTimestamps()方法添加created_at和updated_at。方法支持自定義名字 。使用addSoftDelete 添加軟刪除字段
更多請(qǐng)參考
https://tsy12321.gitbooks.io/phinx-doc/content/writing-migrations-working-with-tables.html
3 執(zhí)行命令
php think migrate:run
執(zhí)行所有遷移文件中的run方法
php think migrate:rollback
回滾命令 執(zhí)行最后執(zhí)行的down命令
總結(jié)
以上是生活随笔為你收集整理的thinkphp之migration 迁移文件的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 前端插件网址
- 下一篇: Winform(C#)输入完毕后,按En