0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【EC-CUBE4.3】product テーブルにカラムを追加する その1

Last updated at Posted at 2025-04-14

【EC-CUBE】product テーブルにカラムを追加する その1

目的

製品(dtb_product)にもフリガナを登録できるようにしたいとのこと。

やること

公式ドキュメントを参考に追加してみる。
https://doc4.ec-cube.net/customize_entity

手順

1. ProductTrait.php を作成

<?php

namespace Customize\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation\EntityExtension;

/**
 * @EntityExtension("Eccube\Entity\Product")
 */
trait ProductTrait
{
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    public $kana;

    public function getKana(): ?string
    {
        return $this->kana;
    }

    public function setKana(?string $kana): self
    {
        $this->kana = $kana;
        return $this;
    }
}

2. Proxy クラスを生成

bin/console eccube:generate:proxies

3. 生成後、定義をデータベースに反映

bin/console cache:clear --no-warmup
bin/console doctrine:schema:update --dump-sql --force

4. マイグレーションファイルを作成

php bin/console doctrine:migrations:generate

5. dtb_product に kana カラムを追加

作成されたマイグレーションファイルに sql を記述。

<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20250414022130 extends AbstractMigration
{
    public function getDescription(): string
    {
        return '';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("ALTER TABLE dtb_product ADD kana VARCHAR(255) DEFAULT NULL AFTER name");
    }

    public function down(Schema $schema): void
    {
        $this->addSql("ALTER TABLE dtb_product DROP kana");
    }
}

6. マイグレーション実行

php bin/console doctrine:migrations:migrate

データベースに「kana」が追加される。(name の後ろ)

その他

先にデータベースの用意をしておいた方がいいかも?

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?