How to Apply Data Patch in Magento 2?
Introduction: What is data patch in Magento 2?
The Data Patch is a class that contains data modification instruction.If we use data patch then all the InstallData and UpgradeData will be replaced.
Apply data patch in Magento 2: Here’s how
Data Patch is defined in <vendor>/<modulename>/setup/patch/data/<patchname>.php and implements Magento\Framework\Setup\Patch\DataPatchInterface.
The following code sample defines a data patch class that has a dependency.
<?php namespace Mageants\Blog\Setup\Patch\Data; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; class PatchDemo implements DataPatchInterface, PatchRevertableInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @param ModuleDataSetupInterface $moduleDataSetup */ public function __construct( ModuleDataSetupInterface $moduleDataSetup ) { $this->moduleDataSetup = $moduleDataSetup; } public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); //Here Write main code logic $this->moduleDataSetup->getConnection()->endSetup(); } public static function getDependencies() { //This functionality notifies Magento to execute the “patches” return []; } public static function getVersion() { //return a version of the patch return ‘1.0.1’; } public function getAliases() { //defines aliases for the patch class return []; } } |
Here,
-apply() function is used to implement the main code logic where we create product attribute code inside the apply() function.
-getDependencies() function contains the class name of dependent patches. This functionality notifies Magento to execute the “patches”.
-getVersion() function will return a version of the patch.
-getAliases() function defines aliases for the patch class. Sometimes, When you want to change the class name then it could be possible using the getAliases() function.
Magento 2 data patch for product attribute
For Create Custom Product Attribute by using patch then Create Customattribute.php file at app\code\Mageants\Blog\Setup\Patch\Data.
<?php namespace Mageants\Blog\Setup\Patch\Data; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchVersionInterface; class Customattribute implements DataPatchInterface,PatchVersionInterface { /** @var ModuleDataSetupInterface */ private $moduleDataSetup; /** @var EavSetupFactory */ private $eavSetupFactory; /** * @param ModuleDataSetupInterface $moduleDataSetup * @param EavSetupFactory $eavSetupFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create([‘setup’ => $this->moduleDataSetup]); $eavSetup->addAttribute(‘catalog_product’, ‘productcustom_attribute’, [ ‘type’ => ‘text’, ‘label’ => ‘Product Custom Attribute’, ‘input’ => ‘text’, ‘visible’ => true, ‘required’ => true, ‘used_in_product_listing’ => true, ‘user_defined’ => true, ]); } public function revert() { $this->moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $eavSetup = $this->eavSetupFactory->create([‘setup’ => $this->moduleDataSetup]); $eavSetup->removeAttribute(‘catalog_product’, ‘productcustom_attribute’); $this->moduleDataSetup->getConnection()->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } public static function getVersion() { return ‘1.0.1’; } public function getAliases() { return []; } } |
By using above code you can see your custom product attribute in eav_attribute table using data patch.
In the above code we have created a ‘productcustom_attribute’ attribute and its type is ‘Text.’
Conclusion:
Using this blog we learn about how to apply data patch in Magento 2.In case of any issue regarding the above example, feel free to ask us.
Also Learn: How to Create Custom Page in Magento 2?
Also Learn: How To Change Admin URL in Magento 2?
Also Learn: How to Create Controller in Magento 2?