40
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP3(Phinx)のMigration記述例

Posted at

CakePHP3はDBスキーマの更新管理にPhinxを採用していますが、ドキュメントが少ない…。
ということで、Migrationファイルの例を書こうと思います。

一応参考:
Cookbook 3.x - Migrations
Phinx - Writing Migrations

テーブルの追加

change()に書ける

$table = $this->table('table_name');
$table->addColumn('name', 'string')
      ->addColumn('description', 'text')
      ->addColumn('created', 'datetime')
      ->addColumn('modified', 'datetime')
      ->create();

テーブル名の変更

change()に書ける

$table = $this->table('old_table_name');
$table->rename('new_table_name');

テーブルの削除

change()に書けない

$this->dropTable('table_name');

カラムの追加

change()に書ける

$table = $this->table('table_name');
$table->addColumn('col_name', 'integer', [
    'default' => 0,
    'limit' => 4,
    'null' => false,
    'after' => 'prev_col_name',
]);
$table->update();

カラム名の変更

change()に書ける

$table = $this->table('table_name');
$table->renameColumn('old_col_name', 'new_col_name');

カラム定義の変更

change()に書けない

$table = $this->table('table_name');
$table->changeColumn('col_name', 'integer', [
    'default' => null,
    'null' => true,
]);

カラムの削除

change()に書けない

$table = $this->table('table_name');
$table->removeColumn('col_name');
40
43
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?