1

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.

0

Changes1:

Check Magento\Framework\Model\AbstractModel::_afterLoad() and as this method signature_afterLoad() does not have any argument,So

return parent::_afterLoad($object);

Should be

return parent::_afterLoad();

Changes2:

Also, You need to protected function _afterLoad(AbstractModel $object) to protected function _afterLoad()

Changes3:

and if (!$object->getIsMassDelete()) {

to

$object = $this;
if (!$object->getIsMassDelete()) {
| improve this answer | |
  • If I remove $object from the return I still 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() because it is used as an input argument. Is there a way to get the object without passing an argument? – stukev Apr 2 at 8:14
  • That worked! Thanks a lot. :) – stukev Apr 2 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.