I've created a backend module to which I've added the possibility to set storeview specific data today. That worked and showed up fine. So I'm trying to load and save my storeview data now but it just won't work. I took the code from this answer and I'm using Magento 2.3.3.
Here is my Model code in which I'm trying to load and save the storeview settings:
<?php
namespace Company\Module\Model;
use Magento\Framework\Model\AbstractModel;
class Module extends AbstractModel
{
/**
* Define resource model
*/
protected function _construct()
{
$this->_init('Company\Module\Model\ResourceModel\Module');
}
/**
* @param \Magento\Framework\Model\AbstractModel $object
* @return void
*/
protected function saveStore($object)
{
$condition = $this->getConnection()->quoteInto('item_id = ?', $object->getId());
$this->getConnection()->delete($this->getTable('company_module_store'), $condition);
foreach ((array)$object->getData('store_id') as $store) {
$storeArray = [
'item_id' => $object->getId(),
'store_id' => $store,
];
$this->getConnection()->insert(
$this->getTable('company_module_store'),
$storeArray
);
}
}
/**
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterSave(AbstractModel $object)
{
if (!$object->getIsMassStatus()) {
$this->saveStore($object);
}
return parent::_afterSave($object);
}
/**
* @param \Magento\Framework\Model\AbstractModel $object
* @return \Magento\Framework\Model\AbstractModel
*/
public function loadStore(AbstractModel $object)
{
$select = $this->getConnection()->select()
->from($this->getTable('company_module_store'))
->where('item_id = ?', $object->getId());
if ($data = $this->getConnection()->fetchAll($select)) {
$array = [];
foreach ($data as $row) {
$array[] = $row['store_id'];
}
$object->setData('store_id', $array);
}
return $object;
}
/**
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterLoad(AbstractModel $object)
{
if (!$object->getIsMassDelete()) {
$this->loadStore($object);
}
return parent::_afterLoad($object);
}
}
When I load this in backend I get:
Exception #0 (Exception): Warning: Declaration of Company\Module\Model\Module::_afterLoad(Magento\Framework\Model\AbstractModel $object) should be compatible with Magento\Framework\Model\AbstractModel::_afterLoad()
I guess the problem is that _afterload originally doesn't accept any arguments? But then how can I solve my problem? Really at a loss here.