- Phalcon - Security Features
- Phalcon - Object Document Mapper
- Phalcon - Working with Forms
- Phalcon - Asset Management
- Phalcon - Multi-Lingual Support
- Phalcon - Session Management
- Phalcon - Cookie Management
- Phalcon - Database Migration
- Phalcon - Query Language
- Phalcon - Scaffolding Application
- Phalcon - Switching Databases
- Phalcon - Database Connectivity
- Phalcon - Routing
- Phalcon - Views
- Phalcon - Models
- Phalcon - Controllers
- Phalcon - Configuration
- Phalcon - Functionality
- Phalcon - Application Structure
- Phalcon - Environmental Setup
- Phalcon - Overview
- Phalcon - Home
Phalcon Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Phalcon - Database Migration
Database migration is important for the following reasons −
Database migration helps in transferring data between the specified storage types.
Database migration refers to the context of web-based apppcations migrating from one platform to another.
This process usually takes place to keep a track of data which is being outdated.
Phalcon performs database migration process in the following way −
Step 1 − Create a project named “dbProject” in xampp/wamp directory.
Step 2 − Configure the project with the appropriate database connectivity.
<?php /* * Modified: preppend directory path of current file, because of this file own different ENV under between Apache and command pne. * 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 PhalconConfig([ database => [ adapter => Mysql , host => localhost , username => root , password => , dbname => demodb , charset => utf8 , ], apppcation => [ appDir => APP_PATH . / , controllersDir => APP_PATH . /controllers/ , modelsDir => APP_PATH . /models/ , migrationsDir => APP_PATH . /migrations/ , viewsDir => APP_PATH . /views/ , pluginsDir => APP_PATH . /plugins/ , pbraryDir => APP_PATH . /pbrary/ , cacheDir => BASE_PATH . /cache/ , baseUri => /dbProject/ , ] ]);
Step 3 − Execute the command for migration of tables included within the database “demodb”. For now, it includes one table “users”.
Step 4 − The database files migrated are stored inside the migrations directory within “app” folder.
Thus, the tables are successfully migrated.
Understanding the Anatomy of Migrated Files
The migrated file has a unique class which extends PhalconMvcModelMigration class. The Migration class in Phalcon includes the methods up() and down(). The up() method is used for performing migration while the down method rolls back the operation.
Users.php
<?php use PhalconDbColumn; use PhalconDbIndex; use PhalconDbReference; use PhalconMvcModelMigration; /** * Class UserMigration_100 */ class UserMigration_100 extends Migration { /** * Define the table structure * * @return void */ pubpc 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 */ pubpc function up() { } /** * Reverse the migrations * * @return void */ pubpc function down() { } }
The class UserMigration_100 as shown in the example above includes associative array with four sections which are −
Columns − Includes a set of table columns.
Indexes − Includes a set of table indexes.
References − Includes all the referential integrity constraints (foreign key).
Options − Array with a set of table creation options.
As seen in the example above, version 1.0.0 of the database was successfully migrated. Phalcon may include and run multiple migration processes depending on how the database content is kept.
Advertisements