Phalcon - 資料庫遷移



資料庫遷移之所以重要,原因如下:

  • 資料庫遷移有助於在指定的儲存型別之間傳輸資料。

  • 資料庫遷移指的是基於 Web 的應用程式從一個平臺遷移到另一個平臺。

  • 此過程通常用於跟蹤已過時的資料。

Phalcon 以以下方式執行資料庫遷移過程:

步驟 1 - 在 xampp/wamp 目錄中建立一個名為 “dbProject” 的專案。

dbproject

步驟 2 - 使用適當的資料庫連線配置專案。

<?php 

/*  
   * Modified: preppend directory path of current file, 
      because of this file own different ENV under between Apache and command line.  
   * NOTE: please remove this comment.  
*/

defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..')); 
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');  
return new \Phalcon\Config(['database' => [
   'adapter' => 'Mysql', 
   'host' => 'localhost', 
   'username' => 'root', 
   'password' => '', 
   'dbname' => 'demodb', 
   'charset' => 'utf8', ],

'application' => [ 'appDir' => APP_PATH . '/', 
   'controllersDir' => APP_PATH . 
   '/controllers/', 'modelsDir' => APP_PATH . 
   '/models/', 'migrationsDir' => APP_PATH . 
   '/migrations/', 'viewsDir' => APP_PATH . 
   '/views/','pluginsDir' => APP_PATH . 
   '/plugins/', 'libraryDir' => APP_PATH . 
   '/library/', 'cacheDir' => BASE_PATH . 
   '/cache/', 'baseUri' => '/dbProject/', 
] ]); 

步驟 3 - 執行命令以遷移資料庫“demodb”中包含的表。目前,它包含一個表“users”。

demodb

步驟 4 - 遷移的資料庫檔案儲存在“app”資料夾中的 migrations 目錄內。

user.php

因此,表已成功遷移。

理解遷移檔案的構成

遷移檔案具有一個唯一的類,該類擴充套件了 Phalcon\Mvc\Model\Migration 類。Phalcon 中的 Migration 類包含 up()down() 方法。up() 方法用於執行遷移,而 down 方法則回滾操作。

Users.php

<?php  

use Phalcon\Db\Column; 
use Phalcon\Db\Index; 
use Phalcon\Db\Reference; 
use Phalcon\Mvc\Model\Migration;  

/**  
   * Class UserMigration_100  
*/ 

class UserMigration_100 extends Migration {     
   /**      
      * Define the table structure      
      *      
      * @return void 
   */      
   public function morph() { 
      $this->morphTable('user', [ 
         'columns' => [ 
            new Column( 'Id', [ 
               'type' => Column::TYPE_INTEGER, 
               'notNull' => true, 
               'autoIncrement' => true, 
               'size' => 11, 'first' => true ] ), 
            new Column( 'username', [ 
               'type' => Column::TYPE_VARCHAR, 
               'notNull' => true, 
               'size' => 40, 
               'after' => 'Id' ] ), 
            new Column( 'email', [ 
               'type' => Column::TYPE_VARCHAR, 
               'notNull' => true, 
               'size' => 40, 
               'after' => 'username' ] ), 
            new Column( 'password', [ 
               'type' => Column::TYPE_VARCHAR, 
               'notNull' => true, 
               'size' => 10, 
               'after' => 'email' ] ) 
         ],  
         'indexes' => [new Index('PRIMARY', ['Id'], 'PRIMARY') ], 
            'options' => [ 'TABLE_TYPE' => 'BASE TABLE', 
               'AUTO_INCREMENT' => '3', 'ENGINE' => 'InnoDB', 
               'TABLE_COLLATION' => 'latin1_swedish_ci' ], 
      ] ); 
   }  
   
   /**      
      * Run the migrations      
      *      * @return void      
   */     

   public function up() {  
   }  

   /**      
      * Reverse the migrations      
      *
      * @return void      
   */
   public function down() {  
   } 
}               

如上例所示,類 UserMigration_100 包含一個關聯陣列,其中包含四個部分:

  • - 包含一組表列。

  • 索引 - 包含一組表索引。

  • 引用 - 包含所有參照完整性約束(外部索引鍵)。

  • 選項 - 包含一組表建立選項的陣列。

如上例所示,資料庫的 1.0.0 版本已成功遷移。根據資料庫內容的維護方式,Phalcon 可能會包含和執行多個遷移過程。

廣告