The UpgradeData script

Let's create the <MAGELICIOUS_DIR>/Core/Setup/UpgradeData.php file with the following content:

use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class UpgradeData implements MagentoFrameworkSetupUpgradeDataInterface {
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.2') < 0) {
$this->upgradeToVersionTwoZeroTwo($setup);
}
$setup->endSetup();
}

private function upgradeToVersionTwoZeroTwo(ModuleDataSetupInterface $setup) {
echo 'UpgradeData->upgradeToVersionTwoZeroTwo()' . PHP_EOL;
}
}

Let's go ahead and replace the echo line with something practical, like adding a new column to an existing table:

$salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
$salesSetup->addAttribute('order', 'merchant_note', [
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'visible' => false,
'required' => false
]);

Here, we used the instance of MagentoSalesSetupSalesSetupFactory, injected through __construct. This further creates an instance of the MagentoSalesSetupSalesSetup class. We need this class in order to create sales EAV attributes. The order entity is somewhat of a strange mix; while it is registered as an EAV type of entity under the eav_entity_type table, it does not really use eav_attribute_* tables – it uses a single sales_order table to store its attributes. We could have easily used (Install|Upgrade)Schema scripts to simply add a new column via $setup->getConnection()->addColumn(). Once executed, this code adds the merchant_note column to the sales_order table. We will use this column later on, as we reach the Extending entities section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.106.69