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

php

注意: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支持在數據庫表上建立外鍵約束。讓咱們在示例表中添加一個外鍵:sql

<?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.json

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

如下是有效的列選項:對於任何列類型:bash

 

For  decimal columns:

For enum and set columns:ide

For integer and biginteger columns:優化

For timestamp columns:ui

朗讀您能夠使用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

相關文章
相關標籤/搜索