亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    教你使用PHP數(shù)據(jù)庫遷移工具“Phinx”

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

    教你使用PHP數(shù)據(jù)庫遷移工具“Phinx”

    文檔地址:https://tsy12321.gitbooks.io/phinx-doc/content

    1.安裝

    composer require nhzex/think-phinx

    2.執(zhí)行

    php vendor/bin/phinx

    直接運(yùn)行 php vendor/bin/phinx init 可生成配置文件

    另外一種方法是直接使用php文件做配置文件

    直接運(yùn)行 php vendor/bin/phinx init 可生成配置文件

    另外一種方法是直接使用php文件做配置文件

    3.使用phinx.php進(jìn)行配置

    <?php $config = array(     'DB_HOST' => 'localhost',     'DB_NAME' => 'root',     'DB_USER' => 'root',     'DB_PWD' => '', ); $settings = $config; #phinx.php <?php require 'db_config.php'; return array(     "paths" => 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

    編輯這個(gè)文件,添加數(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)會(huì)添加一個(gè)自增id,作為主鍵

    執(zhí)行 php vendor/bin/phinx migrate

    8.初始化數(shù)據(jù)

    執(zhí)行 php vendor/bin/phinx seed:create CategorySeeder

    系統(tǒng)自動(dòng)創(chuàng)建 created ./db/seeds/CategorySeeder.php

    9.修改 CategorySeeder.php

    執(zhí)行 php vendor/bin/phinx seed:run 將會(huì)進(jìn)行所有Seed

    10.如果想運(yùn)行指定的Seed需要用- s參數(shù)指定

    php vendor/bin/phinx seed:run -s CategorySeeder

    11.更新表結(jié)構(gòu)

    當(dāng)需要更新表結(jié)構(gòu)的時(shí)候,需要再創(chuàng)建一個(gè)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不會(huì)執(zhí)行, 只會(huì)執(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

    生成文件

    <?php use PhinxSeedAbstractSeed; class UserSeeder extends AbstractSeed {     /**      * 插入數(shù)據(jù)      */     public function run() {         $data = array(           array(               'id'    => 1,           ),           array(               'id'    => 2,           )         );         $posts = $this->table('users');         $posts->insert($data)->save();     } }

    phinx特別適合在開發(fā),測(cè)試,線上數(shù)據(jù)庫同步字段信息,數(shù)據(jù)信息,生成和同步測(cè)試數(shù)據(jù)等,所以特別適合在團(tuán)隊(duì)開發(fā)流程中使用,尤其是對(duì)于一個(gè)新項(xiàng)目,只要在項(xiàng)目的開始就一直堅(jiān)持使用phinx獨(dú)立部署,那么每次變更數(shù)據(jù)庫表信息團(tuán)隊(duì)成員都可以通過git或者svn的方式同步代碼然后執(zhí)行上面提到的執(zhí)行命令來同步庫表信息,以此避免傳統(tǒng)開發(fā)時(shí)不同開發(fā)環(huán)境同步庫表信息的繁瑣和失誤的情況。

    在phinx.php 有一個(gè)配置項(xiàng)”default_migration_table” => “phinxlog” 這里是記錄變更記錄的,這也是保障不會(huì)重復(fù)執(zhí)行的一個(gè)措施,所以不用擔(dān)心丟失或者重復(fù)操作執(zhí)行命令。

    推薦學(xué)習(xí):《PHP視頻教程》

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)