5

I'm using magento verison 2.2.5 and i try to use Setup/UpgradeData.php to run a model function to insert data into database from an external API. but when i run php bin/magento setup:upgrade i always got this error:

Area code not set: Area code must be set before starting a session.

i already set the area code before i run my model function like this:

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    public function __construct(
      \Vendor\Module\Model\Test $test,
      \Magento\Framework\App\State $state
    ){
      $this->test = $test;
      $this->state = $state;
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $test = $this->test;
        if (version_compare($context->getVersion(), "1.0.3", "<")) { 
          $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
          $test->syncBankData();
          $test->syncCUstomerBankData();
        }
    }
}
3
  • Are you using any extension in your project? Aug 2, 2018 at 4:22
  • @RutveeSojitra yes i run this from my custom extension Aug 2, 2018 at 4:29
  • please check my answer, it should work Aug 24, 2018 at 17:05

3 Answers 3

2

You have to use objectManager to initiate classes in your command classes as shown below -:

/**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @var State
     */
    protected $_state;

    /**
     * Construct function for class UpdateSummary
     *
     * @param ObjectManagerInterface $objectManager
     */
    public function __construct(
        ObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
        parent::__construct();
    }

    /**
     * Function to configure the command in magento 2
     *
     * @return void
     */
    protected function configure()
    {
        $this->setName('custom:function');
        $this->setDescription('Custom Function Description');
        parent::configure();
    }

    /**
     * 
     *
     * @param InputInterface  $input
     * @param OutputInterface $output
     * 
     * @return void
     */
    public function execute(InputInterface $input, OutputInterface $output)
    {
        $this->_state = $this->objectManager->get(State::class);
        $this->_helper = $this->objectManager->get(Helper::class);

        $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
        if ($this->_helper->isEnabled()) {
            .....
        } else {
            $output->writeln(__("Module is Disabled. Please enable the module to run this command"));
        }
    }
1

You will have to change area to setup instead of adminhtml, replace

$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);

with

  $this->state->setAreaCode('setup');

Reference -https://github.com/magento/magento2/blob/b366da/setup/src/Magento/Setup/Model/Installer.php#L902-L902

1

It's issued found when we are using third party extension: Change your counsturct code any try if it can help you:

public function __construct(
  \Vendor\Module\Model\Test $test,
  \Magento\Framework\App\State $state
){
  $this->test = $test;
  $this->state = $state;
  $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
}
1
  • still same error Aug 2, 2018 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.