Magento2 エクステンションの作成 その3
Rev.1を表示中。最新版はこちら。
1. 概要
ここではModel関連の以下のクラスを作成する。
- Model
- ResourceModel
- ResourceModel Collection
2. Modelの作成
いわゆるMVCのModelクラスを作成する。今回のエクステンションでは、投稿をbithive_topic_postテーブルで管理するのでPostクラスを作成する。ファイルはModel/Post.phpとして作成する。
Model/Post.php
<?php
namespace BitHive\Topic\Model;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class Post extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
const CACHE_TAG = 'bithive_topic_post';
protected $_cacheTag = 'bithive_topic_post';
protected $_eventPrefix = 'bithive_topic_post';
/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
protected $timezone;
public function __construct(
Context $context,
Registry $registry,
TimezoneInterface $timezone
)
{
$this->timezone = $timezone;
parent::__construct($context, $registry);
}
protected function _construct()
{
// ResouceModelのクラス名
$this->_init(\BitHive\Topic\Model\ResourceModel\Post::class);
}
// IdentityInterfaceのメソッド
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
public function beforeSave()
{
// 更新時のmodifiedを自動設定させるため
$this->setData('modified', null);
return parent::beforeSave();
}
// $this->getData('date') で取得される時刻はUTCなので、
// タイムゾーンを指定して時刻を取得するメソッド
// nullの場合はconfigに従う。
public function getConvertedDate($format = 'Y-m-d H:i:s', $tz = null)
{
$defaultTz = $this->timezone->getDefaultTimezone(); // 'UTC'
$configTz = $this->timezone->getConfigTimezone(); // 'Asia/Tokyo' Websiteの設定が参照される
$tz = $tz ?: $configTz;
$date = $this->getData('date'); // default timezone(UTC)での時刻
$datetime = new \DateTime($date, new \DateTimeZone($defaultTz));
$datetime->setTimezone(new \DateTimeZone($tz));
return $datetime->format($format);
}
}
Modelは\Magento\Framework\Model\AbstractModelの継承と\Magento\Framework\DataObject\IdentityInterfaceインターフェースの実装が必要になる。
3. ResourceModelの作成
4. ResourceModel Collectionの作成
