LoginSignup
10
8

More than 5 years have passed since last update.

Cake3.0のMigrationsプラグインの導入

Last updated at Posted at 2014-11-18

Cake3.0のMigrationsプラグインの導入

前提条件

CakePHP 3.0をインストールする

概要

  • CakeDCからcakephpにGitHubアカウントが移動
  • MigrationsプラグインはPhinxのラッパーに変更
  • まだ現在、pre-alphaである(※仕様等変わる可能性がありますのでご注意ください。)

導入方法

1.インストール

composerからインストールします。

"require": {
    "cakephp/migrations": "dev-master"
}

2.設定

bootstrap.phpに以下を設定する

Plugin::load('Migrations');

3.基本的な使い方

基本的な使い方は以下になります。

php bin/cake.php migrations

4.マイグレーションの作成

マイグレーション名を指定してマイグレーションファイルを作成します。

php bin/cake.php migrations create マイグレーション名

出力されるファイル

<?php

use Phinx\Migration\AbstractMigration;

class Hoge extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-change-method
     *
     * Uncomment this method if you would like to use it.
     *
    public function change()
    {
    }
    */

    /**
     * Migrate Up.
     */
    public function up()
    {
        // ここにupの処理を追記します。
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        // ここにdownの処理を追記します。
    }
}

5.テーブル作成・削除

○以下の内容をupに追記してテーブルを作成します。
 ※primary keyは自動で作成されます。

$table = $this->table('users');
        $table->addColumn('name', 'text')
            ->addColumn('loginid', 'text')
            ->addColumn('password', 'text')
            ->addColumn('etc', 'text', ['null'=>true])
            ->addColumn('created', 'datetime', ['null'=>true])
            ->addColumn('modified', 'datetime', ['null'=>true])
            ->create();

○以下の内容をdownに追記してテーブルを削除します。

$this->dropTable('users');

5.その他

// マイグレーションUP(最新に更新)
php bin/cake.php migrations migrate

// マイグレーションUP(バージョン指定)
php bin/cake.php migrations migrate -t 20150515015053

// マイグレーションDOWN
php bin/cake.php migrations rollback

// マイグレーションDOWN(バージョン指定)
php bin/cake.php migrations rollback -t 20150515015053

// マイグレーションの状態確認
php bin/cake.php migrations status

詳細はPhinxのマニュアル参照

Phinxマニュアル

10
8
2

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
10
8