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();
}
}
}