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-CUBEプラグインで追加データベースにマスターデータ入力

Last updated at Posted at 2024-09-24

基本環境

EC-CUBE4.2.3

関連ファイル

プラグイン名:PluginName
追加したデータベース:plg_plugin_name_hoge
リレーションするデータベース:dtb_huga

Entityで追加データベースを作成

<?php

namespace Plugin\PluginName\Entity;

use Doctrine\ORM\Mapping as ORM;

if (!class_exists('\Plugin\PluginName\Entity\PluginNameHoge')) {
    /**
     * PluginNameHoge
     *
     * @ORM\Table(name="plg_plugin_name_hoge")
     * @ORM\InheritanceType("SINGLE_TABLE")
     * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
     * @ORM\HasLifecycleCallbacks()
     * @ORM\Entity(repositoryClass="Plugin\PluginName\Repository\PluginNameHogeRepository")
     */
    class PluginNameHoge extends \Eccube\Entity\AbstractEntity
    {

    }

初期データを書き込む

root/app/Plugin/PluginName/PluginManager.php
<?php
namespace Plugin\PluginName;

use Eccube\Plugin\AbstractPluginManager;
use Eccube\Entity\Master\AbstractMasterEntity;
use Eccube\Entity\Master\HugaType;
use Plugin\PluginName\Entity\Master\PluginNameHoge;
use Symfony\Component\DependencyInjection\ContainerInterface;


class PluginManager extends AbstractPluginManager

{
     // プラグイン有効化時に走る設定
     /**
     * 
     * @param array $meta
     * @param ContainerInterface $container
     */
    public function enable(array $meta, ContainerInterface $container)
    {
        $this->createPluginNameHoge($container);
    }

     // 設定情報を入れる
     /**
     * 
     * @param ContainerInterface $container
     * 
     * @param \Plugin\PluginName\Entity\Master\Plugin\PluginNameHugaType $id
     */
    private function createPluginNameHoge(ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(PluginNameHoge::class, 1);
        if($Config) return; // すでにINSERTされてたら後続処理を行わない
        $Config = new PluginNameHoge();

        // ここにデータを入れていく
        
        $em->persist($Config);
        $em->flush($Config);
     }

追加するデータ

integer型を入力する

$Config->setSortNo(1);

sort_noというカラムに1というinteger型数値を追加

string型を入力する

$Config->setFieldName('id');

field_nameというカラムにidというstring型文字を追加

string型を入力する(スラッシュ入り)

$Config->setEntityName('Eccube\\\Entity\\\Product');

entity_nameというカラムにEccube\\Entity\\Productというstring型文字を追加
※スラッシュを1個増やす

リレーション型を入力する(その1)

        $em = $container->get('doctrine.orm.entity_manager');
        
        $HugaType = $em->getRepository(HugaType::class)
        ->find(HugaType::HUGA_TYPE_PRODUCT);
        $Config->setHugaType($HugaType);

huga_type_idというカラムにdtb_huga_typeというテーブルのidをリレーションする

以下のType.phpを参照

root/app/Plugin/PluginName/Entity/HugaType.php
class HugaType extends \Eccube\Entity\Master\AbstractMasterEntity
    {
        /**
         * @var integer
         */
        public const CSV_TYPE_PRODUCT = 1;

        /**
         * @var integer
         */
        public const CSV_TYPE_CUSTOMER = 2;

        /**
         * @var integer
         */
        public const CSV_TYPE_ORDER = 3;

        /**
         * @var integer
         */
        public const CSV_TYPE_SHIPPING = 4;

        /**
         * @var integer
         */
        public const CSV_TYPE_CATEGORY = 5;
    }

CSV_TYPE_PRODUCTを設定するとhuga_type_id1がリレーションされる

リレーション型を入力する(その2)

$em = $container->get('doctrine.orm.entity_manager');

$DeliveryId = $em->getRepository(Delivery::class)
->find(1);
$Config->setDelivery($DeliveryId);

delivery_idというカラムにdtb_deliveryというテーブルのid(1)をリレーションする

TWIGファイルURLを入力する

$Config->setFileName('PluginName/Resource/template/default/Hoge/hoge.twig');

デフォルトでEC-CUBE管理画面から編集できるtwigファイルをプラグインで上書きする場合は、
PluginName/Resource/template/default/Hoge/hoge.twig
という書き方をすると、管理画面から編集できる。

初期段階では、
eccube-root/app/Plugin/PluginName/Resource/template/default/Hoge/hoge.twig
から読み込み、管理画面で編集、登録すると、
eccube-root/app/template/default/PluginName/Resource/template/default/Hoge/hoge.twig
にファイルを作成・保存し、次回以降はこちらを編集できる

PluginManager.php

root/app/Plugin/PluginName/PluginManager.php
<?php
namespace Plugin\PluginName;

use Eccube\Plugin\AbstractPluginManager;
use Eccube\Entity\Master\AbstractMasterEntity;
use Eccube\Entity\Master\HugaType;
use Plugin\PluginName\Entity\Master\PluginNameHoge;
use Symfony\Component\DependencyInjection\ContainerInterface;


class PluginManager extends AbstractPluginManager

{
    /**
     * プラグイン有効化時に走る
     * @param array $meta
     * @param ContainerInterface $container
     */
    public function enable(array $meta, ContainerInterface $container)
    {
        $this->createPluginNameHoge($container);
    }

     /**
     * 設定情報を入れる
     * @param ContainerInterface $container
     * 
     * @param \Plugin\PluginName\Entity\Master\Plugin\PluginNameHugaType $id
     */
    private function createPluginNameHoge(ContainerInterface $container)
    {
        $em = $container->get('doctrine.orm.entity_manager');
        $Config = $em->find(PluginNameHoge::class, 1);
        if($Config) return; // すでにINSERTされてたら後続処理を行わない
        $Config = new PluginNameHoge();

        // integer型を入力する
        $Config->setSortNo(1);

        // string型を入力する
        $Config->setFieldName('id');

        // string型を入力する(スラッシュ入り)
        $Config->setEntityName('Eccube\\\Entity\\\Product');

        // 他のデータベースからのリレーションを入力する その1
        $HugaType = $em->getRepository(HugaType::class)
        ->find(HugaType::HUGA_TYPE_PRODUCT);
        $Config->setHugaType($HugaType);

        // 他のデータベースからのリレーションを入力する その2
        $DeliveryId = $em->getRepository(Delivery::class)
        ->find(1);
        $Config->setDelivery($DeliveryId);
        
        $em->persist($Config);
        $em->flush($Config);
     }
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?