前回はプラグインによるデータベースの定義を行いました。
今回は定義したデータベースへの処理を行いたいと思います。
ECサイトを作成する為にはデータベースが重要な割合を占める部分かと思います。
実際にはデータベースへの操作は必須な気もしますのでしっかり理解したいですね。
それでは進めて行きたいと思います。
・
・
・
出来ました。
ファイル自体は前回の内容を流用する形です。
とりあえず、以下の形式で問題ないと思うのですが・・・
■プラグインファイル
config.yml
PluginManager.php
Entity/NakwebTest006.php
Migration/Version20161218094000.php
Repository/NakwebTest006Repository.php
Resource/doctrine/Plugin.NakwebTest006.Entity.NakwebTest006.dcm.yml
ServiceProvider/NakwebTest006ServiceProvider.php
▼config.yml
name: データベース関連確認のプラグイン
code: NakwebTest006
version: 0.0.1
service:
- NakwebTest006ServiceProvider
orm.path:
- /Resource/doctrine
最後に使用する service に関しての記載が増えています。
これからプラグインが複雑化すると記載される内容も増えていきますね。
▼PluginManager.php
<?php
/**
* This file is part of EC-CUBE
*/
namespace Plugin\NakwebTest006;
use Eccube\Plugin\AbstractPluginManager;
class PluginManager extends AbstractPluginManager
{
public function install($config, $app)
{
// データベース作成
$this->migrationSchema( $app, __DIR__ . '/Migration', $config['code'] );
}
public function uninstall($config, $app)
{
// データベース削除
$this->migrationSchema( $app, __DIR__ . '/Migration', $config['code'], 0 );
}
public function enable($config, $app)
{
// プラグイン有効処理
$this->exeInputLog( $app, 'enable' );
}
public function disable($config, $app)
{
// プラグイン無効処理
$this->exeInputLog( $app, 'disable' );
}
public function update($config, $app)
{
}
public function exeInputLog($app, $eventType)
{
// 登録情報
$name = $eventType;
$data = $eventType . ' : ' . date( 'Y/m/d H:i:s' );
$inputData = new \Plugin\NakwebTest006\Entity\NakwebTest006();
$inputData->setName( $name );
$inputData->setData( $data );
// DB更新
$app['orm.em']->persist($inputData);
$app['orm.em']->flush($inputData);
}
}
本来はここでする内容でも無いかもしれませんが、データベースの操作テストとして「有効/無効」のログをデータベースに保存しています。
実際にはエンティティの内容を読み込んでオブジェクトを作成していく形ですね。
▼Entity/NakwebTest006.php
※Entityフォルダに保存しています。
<?php
/**
* This file is part of EC-CUBE
*/
namespace Plugin\NakwebTest006\Entity;
class NakwebTest006 extends \Eccube\Entity\AbstractEntity
{
private $id;
private $name;
private $data;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getData()
{
return $this->data;
}
public function setData($data)
{
$this->data = $data;
return $this;
}
}
ログを記録するときに使用していたエンティティです。
個別にファイルを作成する必要があるのは少々手間かもしれません。
▼Migration/Version20161218094000.php
※Migrationフォルダに保存しています。
<?php
namespace DoctrineMigrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
class Version20161218094000 extends AbstractMigration
{
public function up(Schema $schema)
{
// データベースの削除
$table = $schema->createTable( 'plg_nakweb_test006' );
$table->addColumn(
'plg_test_id',
'integer',
array(
'autoincrement' => true
)
);
$table->addColumn(
'test_name',
'text',
array(
'notnull' => false
)
);
$table->addColumn(
'test_data',
'text',
array()
);
$table->setPrimaryKey( array( 'plg_test_id' ) );
}
public function down(Schema $schema)
{
// データベースの削除
$schema->dropTable( 'plg_nakweb_test006' );
}
}
EC-CUBE内で操作するデータベース定義ですね。
今回はデータ登録の際にオートインクリメントをする設定にしています。
▼Repository/NakwebTest006Repository.php
※Repositoryフォルダに保存しています。
namespace Plugin\NakwebTest006\Repository;
use Doctrine\ORM\EntityRepository;
class NakwebTest006Repository extends EntityRepository
{
}
今回は作成していますが特には何もしていません。
必要との記述を読んだ気はするのですが、フォームが無い場合はあまり意味はないかもしれません。
▼Resource/doctrine/Plugin.NakwebTest006.Entity.NakwebTest006.dcm.yml
※Resource/doctrineフォルダに保存しています。
Plugin\NakwebTest006\Entity\NakwebTest006:
type: entity
table: plg_nakweb_test006
repositoryClass: Plugin\NakwebTest006\Repository\NakwebTest006Repository
id:
id:
type: integer
nullable: false
unsigned: false
id: true
column: plg_test_id
generator:
strategy: AUTO
fields:
name:
type: text
nullable: true
column: test_name
data:
type: text
nullable: true
column: test_data
lifecycleCallbacks: { }
今回の作業で一番重要な部分になります。
データベースの本体とEC-CUBE側のデータオブジェクトの連携を行います。
ここがしっかりしていないとデータベースとの連携はできません。
▼ServiceProvider/NakwebTest006ServiceProvider.php
※ServiceProviderフォルダに保存しています。
<?php
namespace Plugin\NakwebTest006\ServiceProvider;
use Eccube\Application;
use Silex\Application as BaseApplication;
use Silex\ServiceProviderInterface;
class NakwebTest006ServiceProvider implements ServiceProviderInterface
{
public function register(BaseApplication $app)
{
//Repository
$app['nakwebtest006.repository.nakwebtest006'] = $app->share(function () use ($app) {
return $app['orm.em']->getRepository('Plugin\NakwebTest006\Entity\NakwebTest006');
});
}
public function boot(BaseApplication $app)
{
}
}
リポジトリの設定を行います。
今回はリポジトリに特殊な起債はしていないため、EC-CUBE標準の機能をそのまま使用している様ですね。
まぁ、こんな感じでデータべエースへの登録が可能になります。
実際にはフォームからの入力などをするプラグインが多いと思います。
その辺りは次回以降に見ていきたいと思います。
ということで今回はここまでです。











