LoginSignup
5
1

はじめに

こんにちは、エンジニアのkeitaMaxです。

以前Dockerで作成した環境を使って、テーブルを作成しようと思います。

Migrationファイルを作成

以下コマンドでMigrationファイルを作成します。

bin/cake bake migration CreateArticles created modified

すると、src/config/Migration/20240614233400_CreateArticles.phpファイルが作成されます。

20240614233400_CreateArticles.php
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateArticles extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $table = $this->table('articles');
        $table->create();
    }
}

これを以下のように簡単なテーブルを作成してみます。

20240614233400_CreateArticles.php
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class CreateArticles extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change(): void
    {
        $this->table('articles')
            ->addColumn('title', 'text', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('body', 'text', [
                'default' => null,
                'null' => false,
            ])
            ->addColumn('created', 'datetime', [
                'default' => null,
                'null' => true,
            ])
            ->addColumn('modified', 'datetime', [
                'default' => null,
                'null' => true,
            ])->create();
    }
}

テーブルを作成

これを以下のコマンドでテーブルを作成します。

bin/cake migrations migrate

DBを見てみます。

以下のコマンドでDBサーバに入ります。

docker compose exec database bash

その後、設定したusernameとpasswordを入力します。

bash-4.4# mysql -u user -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

mysql> こんな感じのものがあれば問題なく入れています。

以下のコマンドで作成したテーブルを確認してみます。

mysql> use database;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+--------------------+
| Tables_in_database |
+--------------------+
| articles           |
| phinxlog           |
+--------------------+
2 rows in set (0.00 sec)

mysql> 

先ほど作成したarticlesテーブルができていることがわかりました。

おまけ DBの接続設定

cakephpのDBの接続設定は以下のようになっています。

app_local.php

    'Datasources' => [
        'default' => [
            'host' => 'database',
            'port' => '3306',
            'username' => 'user',
            'password' => 'password',
            'database' => 'database',
            'url' => env('DATABASE_URL', null),
        ],

参考にしてください!

おわりに

この記事での質問や、間違っている、もっといい方法があるといったご意見などありましたらご指摘していただけると幸いです。

最後まで読んでいただきありがとうございました!

5
1
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
5
1