EC-CUBE4 での テーブルのカラム変更 の紹介します。
EC-CUBEでのDBのテーブルはEntityとして管理してあるので、テーブルのカラム変更は変更したテーブルに対応するEntityのカスタマイズするということになります。
マイグレーションもありますが、Ruby on Railsのマイグレーションとは違い、EC-CUBEではデータの変更に使用されます。
こちらの記事でその方法を紹介しているのでよかったらご覧ください。EC-CUBE4 マイグレーション 実行と作成
簡単にカラムの追加をしてみましょう。
カラム追加
商品(dtb_product)テーブルに商品の店舗名としてshop_nameを追加してみましょう。
Step1. Traitファイルの作成
app/Customize/Entity にProductTrait.phpとしてカスタマイズファイルを作成します。
内容は以下です。
<?php
namespace Customize\Entity;
use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation\EntityExtension;
/**
* @EntityExtension("Eccube\Entity\Product")
*/
trait ProductTrait
{
/**
* @ORM\Column(name="shop_name", type="string", nullable=true)
*/
public $shop_name;
/**
* Get shop_name
*
* @return shop_name
*/
public function getShopName()
{
return $this->shop_name;
}
/**
* Set shop_name
*
* @return this
*/
public function setFaxNumber($shop_name)
{
$this->shop_name = $shop_name;
}
}
13~16行目で追加したいカラムの属性と名前を指定します。
23~26行目でゲッター、
32~36行目でセッターを記述しています。
Step2. Proxyの生成
次に、proxyファイルを作成していきます。
コマンドはこちら、
$ php bin/console eccube:generate:proxies
PHP Warning: Module 'apcu' already loaded in Unknown on line 0
gen -> /***/app/proxy/entity/src/Eccube/Entity/Product.php
生成されたファイルをみてみましょう。
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Eccube\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="dtb_product")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="Eccube\Repository\ProductRepository")
*/
class Product extends \Eccube\Entity\AbstractEntity
{
use \Customize\Entity\ProductTrait;
作成したTraitファイルがuseされていたらOKです。
Step3. テーブル反映
テーブル反映するために、SQLを実行していきましょう。
その前に、生成されたProxyファイルを確実に読み込ませるようにキャッシュを削除しておきましょう。
$ php bin/console cache:clear --no-warmup
SQL実行コマンドはこちら、
$ php bin/console doctrine:schema:update --dump-sql --force ...<span class="token punctuation">[</span>OK<span class="token punctuation">]</span> Database schema updated successfully<span class="token operator">!</span>
OKがでていれば、完了です!
以上です。お疲れ様でした!
おわりに
最後までご覧いただきありがとうございます。
EC-CUBE4のソースはこちらです。https://www.ec-cube.net/product/4.0/


コメント