Create a migration class, the first letter must be uppercase
php think migrate:create Users
可以看到目录下有新文件 .\database\migrations\20161117144043_users.php
Use Case
<?php use Phinx\Migration\AbstractMigration; class Users extends AbstractMigration { /** * Change Method. */ public function change() { // create the table $table = $this->table('users',array('engine'=>'MyISAM')); $table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用户名,登陆使用')) ->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码')) ->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=> 'Login state' ( 'limit' => 32, 'default' => 0, 'Comment' => 'exclusive login identifier'Array-> the addColumn ( 'login_code', 'String',)) )) -> the addColumn ( 'last_login_ip', 'Integer', Array ( 'limit' =>. 11, 'default' => 0, 'Comment' => 'Last login the IP' )) -> the addColumn ( 'last_login_time', 'datetime', Array ( 'default' => 0, 'Comment' => 'last login time' )) -> the addColumn ( 'is_delete', 'Boolean', Array ( 'limit' =>. 1, 'default' = > 0, 'comment' => ' delete state, a deleted' )) -> AddIndex ( Array ( 'username'), Array ( 'UNIQUE' => to true )) -> Create (); } /** * Migrate Up. */ public function up() { } /** * Migrate Down. */ public function down() { } }
The above description is provided in the manual thinkphp
================================================== ==== dividing line ============================================ ================================
1. Create a data table
Use up and down method
Up method
up method will Phinx automatically executed when the migration command , and before the script was not executed. You should modify the database will be written in this way in the method.
Down Method
down method will automatically execute when Phinx rollback command , and the script has been executed before the migration. You should be rolled back in the code in this method.
Save method
When the operation Table object, Phinx are a few things to change the database. If you do not know what to do this, it is recommended that you use the save method. It will automatically identify the insert or update operations, and changes to the database.
These three methods are recommended when you create a data table!
Field Operations
Field Type
Field types are as follows:
- biginteger
- binary
- boolean
- date
- datetime
- decimal
- float
- integer
- string
- text
- time
- timestamp
- uuid
In addition, MySQL adapter support enum
, set
, blob
and json
( json
need MySQL 5.7 or higher)
Postgres adapter support smallint
, json
, jsonb
and uuid
(need PostgresSQL 9.3 or higher)
2 field options
The following are valid field options:
All fields:
Options | description |
---|---|
limit | Set the maximum length of string |
length | limit aliases |
default | Set Default |
null | Allow air |
after | Placed behind the specified field in which field |
comment | Field Note |
decimal
Type field:
Options | description |
---|---|
precision | And a combination setting precision scale |
scale | Combination setting accuracy and precision |
signed | Unsigned option is turned on or off (only for MySQL) |
enum
And set
type fields:
Options | description |
---|---|
values | Representative values separated by commas |
integer
And biginteger
type fields:
Options | description |
---|---|
identity | Turned on or off from growth |
signed | Unsigned option is turned on or off (only for MySQL) |
timestamp
Type field:
Options | description |
---|---|
default | Set the default value (CURRENT_TIMESTAMP) |
update | When the trigger action (CURRENT_TIMESTAMP) when data is updated |
timezone | Turned on or off with time zone options |
You can use the standard addTimestamps()
addition method created_at
and updated_at
. Support for custom method name. Add soft delete fields using addSoftDelete
Please refer to
https://tsy12321.gitbooks.io/phinx-doc/content/writing-migrations-working-with-tables.html
3 Run
php think migrate:run
The method of execution run all migrated files
php think migrate:rollback
Rollback command last executed command down