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.