1

I am creating a product attribute for sorting which will be int type : where I am getting following error:

PHP Fatal error: Uncaught Error: Class 'Magento\Catalog\Model\Resource\Eav\Attribute' not found in /opt/bitnami/apps/magento/htdocs/app/code
/Vendor/Module/Setup/InstallData.php:57

Here is my Vendor/Module/Setup/InstallData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav/attribute
         */

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sorting_attribute',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Sorting Attribute',
                'input' => '',
                'class' => '',
                'source' => '',
                'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}
2

Change the code in module.xml file to this.

    <?xml version="1.0" encoding="UTF-8"?>
<config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module"  setup_version="1.0.0">
     <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Replace your code with this code in InstallData.php.

namespace Vendor\Module\Setup;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Resource\Eav\Attribute;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;


/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        /* assign object to class global variable for use in other class methods */
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         $setup->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);


        /**
         * Add attributes to the eav/attribute
       */
       $eavSetup->addAttribute(
         \Magento\Catalog\Model\Product::ENTITY,
       'sorting_attribute',
       [
       'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Sorting Attribute',
        'input' => 'text',
        'class' => '',
        'source' => '',
        'global' => 1,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => 0,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => 'simple,configurable,virtual,bundle,downloadable,grouped',
        'group'=> 'General'
       ]
      );






       $setup->endSetup();
    }
}
2

Change: \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL to \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL.

     //'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL, <= Remove this line
     'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
  • after updating your comment in my code m getting : Exception #0 (Magento\Framework\Exception\LocalizedException): The configuration parameter "formElement" is a required for "sorting_attribute" field. – supriya mishra Nov 7 '16 at 7:07
  • Now, your setup script run successfully? Or still throw new exception? – Khoa TruongDinh Nov 7 '16 at 7:11
  • yes I did setup:upgrade still getting the same error – supriya mishra Nov 7 '16 at 7:16
  • this error is coming while clicking on Products->catalog-> add new product tab – supriya mishra Nov 7 '16 at 7:18
  • setup:upgrade still getting error or not? – Khoa TruongDinh Nov 7 '16 at 7:20

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.