2

I am getting an issue on Magento 2.2.6 when trying to save my module record. This error occurs sometimes and the error disappears if run Magento upgrade command. But it comes again sometime.

system.log :

[2019-03-18 04:46:32] main.CRITICAL: Exception message: Invalid method Magento\Eav\Model\Entity\Attribute::isScopeStore
Trace: #0 /var/www/html/test/app/code/VendorName/ModuleName/Model/ResourceModel/EntityName.php(147): Magento\Framework\DataObject->__call('isScopeStore', Array)
#1 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1536): VendorName\ModuleName\Model\ResourceModel\EntityName->_saveAttribute(Object(VendorName\ModuleName\Model\EntityName), Object(Magento\Eav\Model\Entity\Attribute), '0')
#2 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1496): Magento\Eav\Model\Entity\AbstractEntity->_insertAttribute(Object(VendorName\ModuleName\Model\EntityName), Object(Magento\Eav\Model\Entity\Attribute), '0')
#3 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1227): Magento\Eav\Model\Entity\AbstractEntity->_processSaveData(Array)
#4 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1198): Magento\Eav\Model\Entity\AbstractEntity->processSave(Object(VendorName\ModuleName\Model\EntityName))
#5 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Eav\Model\Entity\AbstractEntity->save(Object(VendorName\ModuleName\Model\EntityName))
#6 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(138): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->___callParent('save', Array)
#7 /var/www/html/test/vendor/magento/framework/App/Cache/FlushCacheByTags.php(68): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->Magento\Framework\Interception\{closure}(Object(VendorName\ModuleName\Model\EntityName))
#8 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Framework\App\Cache\FlushCacheByTags->aroundSave(Object(VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor), Object(Closure), Object(VendorName\ModuleName\Model\EntityName))
#9 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(153): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->Magento\Framework\Interception\{closure}(Object(VendorName\ModuleName\Model\EntityName))
#10 /var/www/html/test/generated/code/VendorName/ModuleName/Model/ResourceModel/EntityName/Interceptor.php(468): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->___callPlugins('save', Array, NULL)
#11 /var/www/html/test/vendor/magento/framework/Model/AbstractModel.php(647): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->save(Object(VendorName\ModuleName\Model\EntityName))
#12 /var/www/html/test/app/code/VendorName/ModuleName/Controller/Adminhtml/EntityName/Save.php(86): Magento\Framework\Model\AbstractModel->save()
#13 /var/www/html/test/generated/code/VendorName/ModuleName/Controller/Adminhtml/EntityName/Save/Interceptor.php(24): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save->execute()
#14 /var/www/html/test/vendor/magento/framework/App/Action/Action.php(107): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->execute()
#15 /var/www/html/test/vendor/magento/module-backend/App/AbstractAction.php(235): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#16 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#17 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(138): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->___callParent('dispatch', Array)
#18 /var/www/html/test/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#19 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#20 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(153): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#21 /var/www/html/test/generated/code/VendorName/ModuleName/Controller/Adminhtml/EntityName/Save/Interceptor.php(39): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->___callPlugins('dispatch', Array, NULL)
#22 /var/www/html/test/vendor/magento/framework/App/FrontController.php(55): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#23 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#25 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#26 /var/www/html/test/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#27 /var/www/html/test/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#28 /var/www/html/test/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#29 /var/www/html/test/vendor/magento/framework/App/Bootstrap.php(257): Magento\Framework\App\Http\Interceptor->launch()
#30 /var/www/html/test/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#31 {main} [] []
[2019-03-18 04:47:03] main.CRITICAL: Exception message: Invalid method Magento\Eav\Model\Entity\Attribute::isScopeStore
Trace: #0 /var/www/html/test/app/code/VendorName/ModuleName/Model/ResourceModel/EntityName.php(147): Magento\Framework\DataObject->__call('isScopeStore', Array)
#1 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1536): VendorName\ModuleName\Model\ResourceModel\EntityName->_saveAttribute(Object(VendorName\ModuleName\Model\EntityName), Object(Magento\Eav\Model\Entity\Attribute), '0')
#2 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1496): Magento\Eav\Model\Entity\AbstractEntity->_insertAttribute(Object(VendorName\ModuleName\Model\EntityName), Object(Magento\Eav\Model\Entity\Attribute), '0')
#3 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1227): Magento\Eav\Model\Entity\AbstractEntity->_processSaveData(Array)
#4 /var/www/html/test/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(1198): Magento\Eav\Model\Entity\AbstractEntity->processSave(Object(VendorName\ModuleName\Model\EntityName))
#5 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Eav\Model\Entity\AbstractEntity->save(Object(VendorName\ModuleName\Model\EntityName))
#6 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(138): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->___callParent('save', Array)
#7 /var/www/html/test/vendor/magento/framework/App/Cache/FlushCacheByTags.php(68): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->Magento\Framework\Interception\{closure}(Object(VendorName\ModuleName\Model\EntityName))
#8 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Framework\App\Cache\FlushCacheByTags->aroundSave(Object(VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor), Object(Closure), Object(VendorName\ModuleName\Model\EntityName))
#9 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(153): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->Magento\Framework\Interception\{closure}(Object(VendorName\ModuleName\Model\EntityName))
#10 /var/www/html/test/generated/code/VendorName/ModuleName/Model/ResourceModel/EntityName/Interceptor.php(468): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->___callPlugins('save', Array, NULL)
#11 /var/www/html/test/vendor/magento/framework/Model/AbstractModel.php(647): VendorName\ModuleName\Model\ResourceModel\EntityName\Interceptor->save(Object(VendorName\ModuleName\Model\EntityName))
#12 /var/www/html/test/app/code/VendorName/ModuleName/Controller/Adminhtml/EntityName/Save.php(86): Magento\Framework\Model\AbstractModel->save()
#13 /var/www/html/test/generated/code/VendorName/ModuleName/Controller/Adminhtml/EntityName/Save/Interceptor.php(24): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save->execute()
#14 /var/www/html/test/vendor/magento/framework/App/Action/Action.php(107): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->execute()
#15 /var/www/html/test/vendor/magento/module-backend/App/AbstractAction.php(235): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#16 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Backend\App\AbstractAction->dispatch(Object(Magento\Framework\App\Request\Http))
#17 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(138): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->___callParent('dispatch', Array)
#18 /var/www/html/test/vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#19 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch(Object(VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#20 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(153): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#21 /var/www/html/test/generated/code/VendorName/ModuleName/Controller/Adminhtml/EntityName/Save/Interceptor.php(39): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->___callPlugins('dispatch', Array, NULL)
#22 /var/www/html/test/vendor/magento/framework/App/FrontController.php(55): VendorName\ModuleName\Controller\Adminhtml\EntityName\Save\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#23 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#25 /var/www/html/test/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#26 /var/www/html/test/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#27 /var/www/html/test/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#28 /var/www/html/test/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#29 /var/www/html/test/vendor/magento/framework/App/Bootstrap.php(257): Magento\Framework\App\Http\Interceptor->launch()
#30 /var/www/html/test/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#31 {main} [] []

/app/code/VendorName/ModuleName/Model/ResourceModel/EntityName.php

<?php

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace VendorName\ModuleName\Model\ResourceModel;

use Magento\Eav\Model\Entity\AbstractEntity;
use Magento\Eav\Model\Entity\Context;
use Magento\Framework\DataObject;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use VendorName\ModuleName\Setup\EntitySetup;

class EntityName extends AbstractEntity {
    /**
     * Store id
     *
     * @var int
     */
    protected $_storeId = null;

    /**
     * @var StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * [__construct description]
     * @param Context               $context      [description]
     * @param StoreManagerInterface $storeManager [description]
     * @param array                 $data         [description]
     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->setType(EntitySetup::ENTITY_TYPE_CODE);
        $this->setConnection(EntitySetup::ENTITY_TYPE_CODE . '_read', EntitySetup::ENTITY_TYPE_CODE . '_write');
        $this->_storeManager = $storeManager;
    }

    /**
     * Set attribute set id and entity type id value
     *
     * @param \Magento\Framework\DataObject $customer
     * @return $this
     * @throws \Magento\Framework\Exception\LocalizedException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    protected function _beforeSave(\Magento\Framework\DataObject $object) {
        $object->setAttributeSetId($object->getAttributeSetId() ?: $this->getEntityType()->getDefaultAttributeSetId());
        $object->setEntityTypeId($object->getEntityTypeId() ?: $this->getEntityType()->getEntityTypeId());
        return parent::_beforeSave($object);
    }

    /**
     * Return Entity Type instance
     *
     * @return \Magento\Eav\Model\Entity\Type
     */
    public function getEntityType() {
        if (empty($this->_type)) {
            $this->setType(EntitySetup::ENTITY_TYPE_CODE);
        }
        return parent::getEntityType();
    }

    /**
     * Retrieve ProductFaqNew entity default attributes
     *
     * @return string[]
     */
    protected function _getDefaultAttributes() {
        return [
            'attribute_set_id',
            'entity_type_id',
            '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(),
            'entity_type_id' => $object->getEntityTypeId(),
            '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;
    }
}

what should I need to change ?

Any help would be appriciated.

Thanks in advance.

  • @HelgeB Do you know about this how to solve it? – Rohan Hapani Apr 25 '19 at 11:58
0

Try to clear "EAV types and attributes" cache. Within admin panel System->Tols->Cache Management-> EAV types and attributes or php bin/magento cache:clean within CLI

| improve this answer | |

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.