| Current Path : /var/www/html/app/code/FlipsDigital/TextbookList/Model/ResourceModel/ |
| Current File : /var/www/html/app/code/FlipsDigital/TextbookList/Model/ResourceModel/TextbookList.php |
<?php
/**
* TextbookList.php
*
* @copyright Copyright © 2021 FlipsDigital. All rights reserved.
* @author calvin.so@flipsdigital.com
*/
namespace FlipsDigital\TextbookList\Model\ResourceModel;
use Magento\Eav\Model\Entity\AbstractEntity;
use Magento\Eav\Model\Entity\Context;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use FlipsDigital\TextbookList\Setup\TextbookListSetup;
class TextbookList extends AbstractEntity
{
/**
* Store id
*
* @var int
*/
protected $_storeId = null;
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* TextbookList constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param array $data
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
array $data = []
) {
parent::__construct($context, $data);
$this->setType(TextbookListSetup::ENTITY_TYPE_CODE);
$this->setConnection(TextbookListSetup::ENTITY_TYPE_CODE . '_read', TextbookListSetup::ENTITY_TYPE_CODE . '_write');
$this->_storeManager = $storeManager;
}
/**
* Retrieve TextbookList entity default attributes
*
* @return string[]
*/
protected function _getDefaultAttributes()
{
return [
'created_at',
'updated_at'
];
}
/**
* Set store Id
*
* @param integer $storeId
* @return $this
*/
public function setStoreId($storeId)
{
$this->_storeId = $storeId;
return $this;
}
/**
* Return store id
*
* @return integer
*/
public function getStoreId()
{
if ($this->_storeId === null) {
return $this->_storeManager->getStore()->getId();
}
return $this->_storeId;
}
/**
* Set Attribute values to be saved
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @param mixed $value
* @return $this
*/
protected function _saveAttribute($object, $attribute, $value)
{
$table = $attribute->getBackend()->getTable();
if (!isset($this->_attributeValuesToSave[$table])) {
$this->_attributeValuesToSave[$table] = [];
}
$entityIdField = $attribute->getBackend()->getEntityIdField();
$storeId = $object->getStoreId()?:Store::DEFAULT_STORE_ID;
$data = [
$entityIdField => $object->getId(),
'attribute_id' => $attribute->getId(),
'value' => $this->_prepareValueForSave($value, $attribute),
'store_id' => $storeId,
];
if (!$this->getEntityTable() || $this->getEntityTable() == \Magento\Eav\Model\Entity::DEFAULT_ENTITY_TABLE) {
$data['entity_type_id'] = $object->getEntityTypeId();
}
if ($attribute->isScopeStore()) {
/**
* Update attribute value for store
*/
$this->_attributeValuesToSave[$table][] = $data;
} elseif ($attribute->isScopeWebsite() && $storeId != Store::DEFAULT_STORE_ID) {
/**
* Update attribute value for website
*/
$storeIds = $this->_storeManager->getStore($storeId)->getWebsite()->getStoreIds(true);
foreach ($storeIds as $storeId) {
$data['store_id'] = (int)$storeId;
$this->_attributeValuesToSave[$table][] = $data;
}
} else {
/**
* Update global attribute value
*/
$data['store_id'] = Store::DEFAULT_STORE_ID;
$this->_attributeValuesToSave[$table][] = $data;
}
return $this;
}
}