Skip to content

Magento 2 : Invalid method MagentoEavModelEntityAttribute::isScopeStore

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

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

<?php

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

namespace VendorNameModuleNameModelResourceModel;

use MagentoEavModelEntityAbstractEntity;
use MagentoEavModelEntityContext;
use MagentoFrameworkDataObject;
use MagentoStoreModelStore;
use MagentoStoreModelStoreManagerInterface;
use VendorNameModuleNameSetupEntitySetup;

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 MagentoFrameworkDataObject $customer
     * @return $this
     * @throws MagentoFrameworkExceptionLocalizedException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    protected function _beforeSave(MagentoFrameworkDataObject $object) {
        $object->setAttributeSetId($object->getAttributeSetId() ?: $this->getEntityType()->getDefaultAttributeSetId());
        $object->setEntityTypeId($object->getEntityTypeId() ?: $this->getEntityType()->getEntityTypeId());
        return parent::_beforeSave($object);
    }

    /**
     * Return Entity Type instance
     *
     * @return MagentoEavModelEntityType
     */
    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 MagentoFrameworkModelAbstractModel $object
     * @param MagentoEavModelEntityAttributeAbstractAttribute $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() == MagentoEavModelEntity::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.