数据库迁移很重要,原因如下:
数据库迁移有助于在指定的存储类型之间传输数据.
数据库迁移是指基于Web的应用程序从一个平台迁移到另一个平台的上下文.
这个过程通常用于跟踪过时的数据.
Phalcon执行数据库迁移过程以下方式 :
第1步 : 在 xampp/wamp 目录中创建名为"dbProject"的项目.
第2步 : 使用适当的数据库连接配置项目.
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".
第4步 : 迁移的数据库文件存储在"app"文件夹中的迁移目录中.
因此,表已成功迁移.
了解迁移文件的剖析
迁移的文件具有唯一的类它扩展了 Phalcon\Mvc\Model\Migration 类. Phalcon中的Migration类包括方法 up()和 down(). up()方法用于执行迁移,而down方法回滚操作.
Users.php
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可能包含并运行多个迁移过程,具体取决于数据库内容的保存方式.