5 years ago (2015-08-26)  Technical application |   First to comment  58 
post score 0 times, average 0.0

This article has been translated by the ultimate guest since: http://phinx.readthedocs.org, bad place, please correct me; series of articles please visit Wang Baiyuan's blog Phinx tab or use geek search: Phinx Get all article directories

To convert databases: To restore or modify data, Phinx is implemented through a series of Migrations files.Each Migrations file is actually a PHP class. The best way to trap a migration is through the API provided by Phinx. Of course, running native SQL statements is also possible.

New Phinx Migration

Create a new Phinx Migration migration.We want to run Phinx's create command in the project folder:

After running the above command, the migration file (this folder is the migrations folder in which you installed phinx) will generate a PHP file named YYYYMMDDHHMMSS_my_new_migration.php with the time stamp of the current 14 characters in the first 14 characters.Phinx automatically creates a framework migration file with two empty and a commented out methods:

[Translation] build migration -up,down,change method -Phinx tutorial (2)

AbstractMigration class

All Phinx Migration migration classes inherit from the AbstractMigration class, which provides the necessary support to create database migrations.Database migration can create new tables, insert rows, add indexes, and modify columns.

Up() method

When you migrate up the database (that is, make new changes to the database) and phinx is not running before it detects the build migrations, the up() method is automatically run by phinx.In the up() method you need to write your own code to modify the database based on your intentions. 

Down() method

When you perform a downward migration of the database (that is, a restore operation of the database), and phinx detects that the constructed migrations have not been executed before, the Down() method is automatically executed by Phinx.You should use the down() method to reverse/undo database modifications.

Change() method

Phinx 0.2.0 introduces a new feature called reversible migration.You only need to define the logic of the up() method of the reversible migration. Then Phinx can automatically find out how to migrate downward for you.Defining a reversible migration must cancel the comment of the change method, and the method definition in the completion.E.g:

When this migration is performed, Phinx will create the user_logins table and automatically implement how to delete the table.Note that Phinx automatically ignores the up() and down() methods when there is a change method in the migration file.If you also need to use these methods, it is recommended that you create a separate migration file.


The create() and update() methods on the table must be used within the create() or update() methods of the change() method.Phinx cannot automatically determine that the save() call creates a new table or modifies an existing one.Phinx's reversible migration can only reverse the following operations:

  • New table
  • Rename table
  • Add column attributes
  • Rename column properties
  • Add an index
  • Add foreign key

If an operation cannot be reversed, Phinx will throw an IrreversibleMigrationException when migrating downwards.


This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!

Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/migration-build-up-down-change-method-phinx-tutorial-2-2.html

Post comment


No Comment


Forget password?