1. 程式人生 > >thinkphp 遷移資料庫 -Phinx 簡單說明文件

thinkphp 遷移資料庫 -Phinx 簡單說明文件

php think 
migrate
  migrate:create     Create a new migration ///建立
  migrate:rollback   Rollback the last or to a specific migration //回滾
  migrate:run Migrate the database //執行 migrate:status Show migration status //狀態檢視 optimize optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded wit h classmaps too, good for production.//朗讀優化PSR0和PSR4軟體包,也可以通過類對映載入,有利於生產。 optimize:config Build config and common file cache.//構建公共配置檔案快取 optimize:route Build route cache.//構建路由快取 optimize:schema Build database schema cache. //構建資料庫構建快取 seed seed:create Create a new database seeder //建立新的資料填充器 seed:run Run database seeders //執行填充器
#建立遷移類,首字母必須為大寫
php think migrate:create Users

參考: http://docs.phinx.org/en/latest/index.html

注意:

  1.  Please be aware that when a change method exists, Phinx will automatically ignore the up and down methods. If you need to use these methods it is recommended to create a separate migration file.
    當change方法存在時,將會自動忽略up /down 方法,如果想要生效需要單獨建立檔案;
  2. When creating or updating tables inside a change() method you must use the Table create()and update() methods. Phinx cannot automatically determine whether a save() call is creating a new table or modifying an existing one.
    change()方法內建立或更新表時,必須使用表create()update()
    方法。Phinx無法自動確定save()是建立新表還是修改現有表。
  3. Phinx 只能撤銷 createTable / renameTable / addColumn / renameColumn / addIndex / addForeignKey 命令;

—— Phinx支援在資料庫表上建立外來鍵約束。讓我們在示例表中新增一個外來鍵:

<?php

use Phinx\Migration\AbstractMigration;

class MyNewMigration extends AbstractMigration
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('tags');
        $table->addColumn('tag_name', 'string')
              ->save();

        $refTable = $this->table('tag_relationships');
        $refTable->addColumn('tag_id', 'integer', ['null' => true])
                 ->addForeignKey('tag_id', 'tags', 'id', ['delete'=> 'SET_NULL', 'update'=> 'NO_ACTION'])
                 ->save();

    }

    /**
     * Migrate Down.
     */
    public function down()
    {

    }
}

  

——有效列型別

In addition, the MySQL adapter supports enumset and blob column types.

In addition, the Postgres adapter supports jsonjsonbuuidcidrinet and macaddr column types (PostgreSQL 9.3 and above).

以下是有效的列選項:對於任何列型別:

 

For  decimal columns:

For enum and set columns:

For integer and biginteger columns:

For timestamp columns:

朗讀您可以使用addTimestamps()方法將created_at和updated_at時間戳新增到表中。此方法還允許您提供替代名稱。可選的第三個引數允許您更改要新增的列的時區選項。此外,您可以使用addTimestampsWithTimezone()方法,該方法是addTimestamps()的別名,它始終將第三個引數設定為true(請參閱下面的示例)。

<?php

use Phinx\Migration\AbstractMigration;

class MyNewMigration extends AbstractMigration
{
    /**
     * Migrate Change.
     */
    public function change()
    {
        // Use defaults (without timezones)
        $table = $this->table('users')->addTimestamps()->create();
        // Use defaults (with timezones)
        $table = $this->table('users')->addTimestampsWithTimezone()->create();

        // Override the 'created_at' column name with 'recorded_at'.
        $table = $this->table('books')->addTimestamps('recorded_at')->create();

        // Override the 'updated_at' column name with 'amended_at', preserving timezones.
        // The two lines below do the same, the second one is simply cleaner.
        $table = $this->table('books')->addTimestamps(null, 'amended_at', true)->create();
        $table = $this->table('users')->addTimestampsWithTimezone(null, 'amended_at')->create();
    }
}

  

For boolean columns:

For string and text columns:

For foreign key definitions:

Limit Option and MySQL

When using the MySQL adapter, additional hinting of database column type can be made for integertext and blob columns. Using limit with one the following options will modify the column type accordingly:

Limit Column Type
BLOB_TINY TINYBLOB
BLOB_REGULAR BLOB
BLOB_MEDIUM MEDIUMBLOB
BLOB_LONG LONGBLOB
TEXT_TINY TINYTEXT
TEXT_REGULAR TEXT
TEXT_MEDIUM MEDIUMTEXT
TEXT_LONG LONGTEXT
INT_TINY TINYINT
INT_SMALL SMALLINT
INT_MEDIUM MEDIUMINT
INT_REGULAR INT
INT_BIG BIGINT
use Phinx\Db\Adapter\MysqlAdapter;

//...

$table = $this->table('cart_items');
$table->addColumn('user_id', 'integer')
      ->addColumn('product_id', 'integer', ['limit' => MysqlAdapter::INT_BIG])
      ->addColumn('subtype_id', 'integer', ['limit' => MysqlAdapter::INT_SMALL])
      ->addColumn('quantity', 'integer', ['limit' => MysqlAdapter::INT_TINY])
      ->create();

  

Get a column list
$columns = $this->table('users')->getColumns();

Get a column by name
$column = $this->table('users')->getColumn('email');

Checking whether a column exists   檢查列是否存在

<?php

use Phinx\Migration\AbstractMigration;

class MyNewMigration extends AbstractMigration
{
    /**
     * Change Method.
     */
    public function change()
    {
        $table = $this->table('user');
        $column = $table->hasColumn('username');

        if ($column) {
            // do something
        }

    }
}

 

  

更多檢視:http://docs.phinx.org/en/latest/migrations.html#working-with-columns