新聞中心
本篇文章給大家分享關(guān)于php處理中數(shù)據(jù)庫遷移工具Phinx的相關(guān)知識,phinx特別適合在開發(fā)、測試、線上數(shù)據(jù)庫同步字段信息、數(shù)據(jù)信息、生成和同步測試數(shù)據(jù)等,希望對大家有幫助。

我們注重客戶提出的每個要求,我們充分考慮每一個細(xì)節(jié),我們積極的做好網(wǎng)站設(shè)計、網(wǎng)站制作服務(wù),我們努力開拓更好的視野,通過不懈的努力,成都創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽,這一切,也不斷的激勵著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計,成都微信小程序,網(wǎng)站開發(fā),技術(shù)開發(fā)實力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。
文檔地址:https://tsy12321.gitbooks.io/phinx-doc/content
1.安裝
composer require nhzex/think-phinx
2.執(zhí)行
php vendor/bin/phinx
直接運行 php vendor/bin/phinx init 可生成配置文件
另外一種方法是直接使用php文件做配置文件
直接運行 php vendor/bin/phinx init 可生成配置文件
另外一種方法是直接使用php文件做配置文件
3.使用phinx.php進(jìn)行配置
'localhost',
'DB_NAME' => 'root',
'DB_USER' => 'root',
'DB_PWD' => '',
);
$settings = $config;
#phinx.php
array(
"migrations" => "db/migrations",
"seeds" => "db/seeds"
),
"environments" => array(
"defaut_migration_table" => "phinxlog",
"default_database" => "lleg",
"default_environment" => "development"
"production" => array(
"adapter" => "mysql",
"host" => $settings["DB_HOST"],
"name" => $settings["DB_NAME"],
"user" => $settings["DB_USER"],
"pass" => $settings["DB_PWD"],
"port" => 3306,
"charset" => "utf8"
),
"development" => array(
"adapter" => "mysql",
"host" => $settings["DB_HOST"],
"name" => $settings["DB_NAME"],
"user" => $settings["DB_USER"],
"pass" => $settings["DB_PWD"],
"port" => 3306,
"charset" => "utf8"
)
)
);
4.執(zhí)行 php vendor/bin/phinx status 查看連接狀態(tài)
5.執(zhí)行 php vendor/bin/phinx create migration
6.現(xiàn)在生成了created /db/migrations/20180310020523_migration.php
編輯這個文件,添加數(shù)據(jù)庫創(chuàng)建內(nèi)容.
public function change() {
$user = $this->table('user');
$user->addColumn('open_id', 'string', ['limit'=>64]);
$user->addColumn('register_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']);
$user->addColumn('favorite_music', 'integer', ['default'=> 0, 'comment'=>'喜歡的音樂']);
$user->addColumn('favorite_vedio', 'integer', ['default'=> 0, 'comment'=>'喜歡的視頻數(shù)']);
$user->addColumn('favorite_article', 'integer', ['default'=> 0, 'comment'=>'喜歡的文章數(shù)']);
$user->addColumn('baby_birthday', 'date', ['null'=>true, 'comment'=>'寶寶生日']);
$user->addColumn('baby_sex', 'boolean', ['null'=>true, 'comment'=>'寶寶性別']);
$user->addColumn('last_login', 'datetime', ['null'=>true, 'comment'=>'最后登陸日期']);
$user->save();
}
7.默認(rèn)會添加一個自增id,作為主鍵
執(zhí)行 php vendor/bin/phinx migrate
8.初始化數(shù)據(jù)
執(zhí)行 php vendor/bin/phinx seed:create CategorySeeder
系統(tǒng)自動創(chuàng)建 created ./db/seeds/CategorySeeder.php
9.修改 CategorySeeder.php
執(zhí)行 php vendor/bin/phinx seed:run 將會進(jìn)行所有Seed
10.如果想運行指定的Seed需要用- s參數(shù)指定
php vendor/bin/phinx seed:run -s CategorySeeder
11.更新表結(jié)構(gòu)
當(dāng)需要更新表結(jié)構(gòu)的時候,需要再創(chuàng)建一個migrate
執(zhí)行php vendor/bin/phinx create ChangeArtist
再將需要更新的內(nèi)容寫到change函數(shù)
public function change() {
$this->execute('alter table resource drop column artist ;');
$resource = $this->table('resource');
$resource->addColumn('artist', 'string', ['limit'=>128, 'default'=>'']);
$resource->update();
}
最后執(zhí)行php vendor/bin/phinx migrate
之前的已經(jīng)執(zhí)行過的migrate不會執(zhí)行, 只會執(zhí)行更新的部分。
12.回滾
php vendor/bin/phinx rollback
13.數(shù)據(jù)填充
php vendor/bin/phinx seed:create UserSeeder php vendor/bin/phinx seed:run -e product
生成文件
1,
),
array(
'id' => 2,
)
);
$posts = $this->table('users');
$posts->insert($data)->save();
}
}
phinx特別適合在開發(fā),測試,線上數(shù)據(jù)庫同步字段信息,數(shù)據(jù)信息,生成和同步測試數(shù)據(jù)等,所以特別適合在團(tuán)隊開發(fā)流程中使用,尤其是對于一個新項目,只要在項目的開始就一直堅持使用phinx獨立部署,那么每次變更數(shù)據(jù)庫表信息團(tuán)隊成員都可以通過git或者svn的方式同步代碼然后執(zhí)行上面提到的執(zhí)行命令來同步庫表信息,以此避免傳統(tǒng)開發(fā)時不同開發(fā)環(huán)境同步庫表信息的繁瑣和失誤的情況。
在phinx.php 有一個配置項”default_migration_table” => “phinxlog” 這里是記錄變更記錄的,這也是保障不會重復(fù)執(zhí)行的一個措施,所以不用擔(dān)心丟失或者重復(fù)操作執(zhí)行命令。
新聞名稱:教你使用PHP數(shù)據(jù)庫遷移工具“Phinx”
路徑分享:http://www.dlmjj.cn/article/dhjsipg.html


咨詢
建站咨詢
