本記事では、cakePHPで、既存データベースを利用する時に、マイグレーションファイルを生成し、そのファイルからモデルを作成する内容となっています。
既存データベースから、マイグレーションファイルの生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[hogehoge@sv7108 app]$ bin/cake bake migration_snapshot Initial Welcome to CakePHP v3.1.14 Console --------------------------------------------------------------- App : src Path: /home/hogehoge/example.com/public_html/sandbox/app/src/ PHP : 5.4.16 --------------------------------------------------------------- Creating file /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190123030101_Initial.php Wrote `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/20190123030101_Initial.php` Marking the migration 20190123030101_Initial as migrated... using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds Migration "20190123030101_Initial.php" has the same name as "20190123025940_Initial.php" Creating a dump of the new database state... using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds Writing dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock`... Dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock` was successfully written [hogehoge@sv7108 app]$ |
マイグレーションファイルの構成
1 2 3 4 5 |
/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations |--20190123030101_Initial.php |--schema-dump-default.lock |
マイグレーションファイルから、テーブルを作成する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[hogehoge@sv7108 app]$ php bin/cake.php migrations migrate Welcome to CakePHP v3.1.14 Console --------------------------------------------------------------- App : src Path: /home/hogehoge/example.com/public_html/sandbox/app/src/ PHP : 5.4.16 --------------------------------------------------------------- using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds using environment default using adapter mysql using database hogehoge_cakephp == 20190123030101 Initial: migrating == 20190123030101 Initial: migrated 0.0195s All Done. Took 0.0213s using migration path /home/hogehoge/example.com/public_html/sandbox/app/config/Migrations using seed path /home/hogehoge/example.com/public_html/sandbox/app/config/Seeds Writing dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock`... Dump file `/home/hogehoge/example.com/public_html/sandbox/app/config/Migrations/schema-dump-default.lock` was successfully written |