0

I have created a custom command, when I run upgrade command to install that module getting following error

In State.php line 153: Area code is not set

Following is my code

use Magento\Framework\App\ResourceConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UninstallPaymentMethod extends Command { const SETUP_MODULE = 'setup_module'; protected $eavSetupFactory; private $state;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
    ResourceConnection $resourceConnection,
    \Magento\Framework\App\State $state
)
{
    parent::__construct('my:first:command');
    $this->eavSetupFactory = $eavSetupFactory;
    $this->resourceConnection = $resourceConnection;
    $this->state = $state;
}

protected function configure() { $this->setName('uninstallpaymentmethod:test'); $this->setDescription('Removing custom customer attribute entry & setup module entry');

   parent::configure();

} protected function execute(InputInterface $input, OutputInterface $output) { $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND); try {

    $connection  = $this->resourceConnection->getConnection();
    $tableName = $connection->getTableName(self::SETUP_MODULE);

    $whereConditions = [
      $connection->quoteInto('module = ?','Test_Test'),
    ];


    $connection->delete($tableName, $whereConditions);

    $eavSetup = $this->eavSetupFactory->create();

    $entityTypeId = 1; // Find these in the eav_entity_type table
    $eavSetup->removeAttribute($entityTypeId, 'test1');
    $eavSetup->removeAttribute($entityTypeId, 'test2');

    $output->writeln("Removed module attributes successfully.");

   } catch (Exception $e) {
       $output->writeln("There was some issue in removing attibute.");
   }

} }

Can someone guide me on where am I wrong? Thanks.

Arjun
  • 3,576
  • 23
  • 59

2 Answers2

0

Try this code

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UninstallPaymentMethod extends Command {

const SETUP_MODULE = 'setup_module';
protected $eavSetupFactory;
private $state;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
    ResourceConnection $resourceConnection,
    ModuleDataSetupInterface $setup,
    \Magento\Framework\App\State $state
) {
    parent::__construct();
    $this->eavSetupFactory = $eavSetupFactory;
    $this->resourceConnection = $resourceConnection;
    $this->setup = $setup;
}

protected function configure()
{
    $this->setName('uninstallpaymentmethod:test');
    $this->setDescription('Removing custom customer attribute entry & setup module entry');

    parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
    try {

        $this->setup->startSetup();

        $connection = $this->resourceConnection->getConnection();
        $tableName = $connection->getTableName(self::SETUP_MODULE);

        $whereConditions = [
            $connection->quoteInto('module = ?', 'VendoreName_ModuleName'),
        ];

        $connection->delete($tableName, $whereConditions);

        $eavSetup = $this->eavSetupFactory->create();

        // For Remove Customer Attribute
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'ATTRIBUTE_CODE');

        // For Remove Product Attribute
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'ATTRIBUTE_CODE');

        // For Remove Category Attribute
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'ATTRIBUTE_CODE');

        $this->setup->endSetup();

        $output->writeln("Removed module attributes successfully.");

    } catch (Exception $e) {
        $output->writeln("There was some issue in removing attibute.");
    }
}

}

Run Magento 2 commands

php bin/magento module:disable VendoreName_ModuleName

php bin/magento uninstallpaymentmethod:test

php bin/magento s:up

Msquare
  • 9,063
  • 7
  • 25
  • 63
0
use Magento\Framework\App\ResourceConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class UninstallPaymentMethod extends Command
{
    const SETUP_MODULE = 'setup_module';
    protected $eavSetupFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        ResourceConnection $resourceConnection
    )
    {
        parent::__construct();
        $this->eavSetupFactory = $eavSetupFactory;
        $this->resourceConnection = $resourceConnection;
    }

   protected function configure()
   {
       $this->setName('uninstallpaymentmethod:test');
       $this->setDescription('Removing custom customer attribute entry & setup module entry');

       parent::configure();
   }
   protected function execute(InputInterface $input, OutputInterface $output)
   {   
       try {

        $connection  = $this->resourceConnection->getConnection();
        $tableName = $connection->getTableName(self::SETUP_MODULE);

        $whereConditions = [
          $connection->quoteInto('module = ?','Test_Test'),
        ];


        $connection->delete($tableName, $whereConditions);

        $eavSetup = $this->eavSetupFactory->create();

        $entityTypeId = 1; // Find these in the eav_entity_type table
        $eavSetup->removeAttribute($entityTypeId, 'test1');
        $eavSetup->removeAttribute($entityTypeId, 'test2');

        $output->writeln("Removed module attributes successfully.");

       } catch (Exception $e) {
           $output->writeln("There was some issue in removing attibute.");
       }
   }
}

This worked for me.

Arjun
  • 3,576
  • 23
  • 59