【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 の後ろ)
その他
先にデータベースの用意をしておいた方がいいかも?