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"文件夹内的迁移目录中。

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 包含一个关联数组,该数组包含四个部分,分别是 −

  • Columns − 包含一组表列。

  • Indexes − 包含一组表索引。

  • References − 包含所有参照完整性约束(外键)。

  • Options − 包含一组表创建选项的数组。

如上例所示,数据库版本 1.0.0 已成功迁移。Phalcon 可能包含并运行多个迁移过程,具体取决于数据库内容的保存方式。