2

My Upgrade Script :

<?php
namespace MyCompany\MyModule\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;

class UpgradeSchema implements UpgradeSchemaInterface
{
    private $categorySetupFactory;

    public function __construct(CategorySetupFactory $categorySetupFactory, EavSetupFactory $eavSetupFactory)
    {
        $this->categorySetupFactory = $categorySetupFactory;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        //handle all possible upgrade versions

        if(!$context->getVersion()) {
        //no previous version found, installation, InstallSchema was just executed
        //be careful, since everything below is true for installation !
        }

        if (version_compare($context->getVersion(), '1.0.2') < 0)
        {
            //code to upgrade to 1.0.2
            $installer = $setup;

            $installer->startSetup();

            $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

            $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);

            $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);

            $categorySetup->removeAttribute(
                \Magento\Catalog\Model\Category::ENTITY, 'my_attribute`enter code here`' );

            $categorySetup->addAttribute(
                \Magento\Catalog\Model\Category::ENTITY, 'my_attribute', [
                     'type' => 'int',
                     'label' => 'MyAttribute',
                     'input' => 'select',
                     'source' => 'MyCompany\MyModule\Model\Api\Category\Attributeset',
                     'required' => false,
                     'sort_order' => 1,
                     'visible'       => 1,
                     'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                     'group' => 'General',
                ]
            );

            $installer->endSetup();
        }
        $setup->endSetup();
    }
 }
 ?>

After setup:upgrade I Got error in composer:

Fatal Error: Declaration of MyCompany\Mymodule\Setup\UpgradeSchema::upgrade() must be compatible with Magento\Framework\Setup\UpgradeSchemaInterface::upgrade()

3

add use Magento\Framework\Setup\SchemaSetupInterface

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.