Kể từ phiên bản 2.3, Magento giới thiệu Data Patch để sửa đổi dữ liệu như là một thay thế cho InstallData và UpgradeData. Một Data Patch được đặt trong Vendor/Module_Name/Setup/Patch/Data/Patch_Name.php và implements MagentoFrameworkSetupPatchDataPatchInterface. Trong bài viết này chúng ta sẽ cùng nhau đi tìm hiểu về cách sử dụng Data Patch trong Magento 2.
Không giống như declarative schema, Patch chỉ được áp dụng một lần và được lưu trữ trong bảng patch_list trong cơ sở dữ liệu. Những Patch chưa được áp dụng sẽ được áp dụng khi chạy câu lệnh php bin/magento setup:upgrade.
Tạo Data Patch trong Magento 2
Để tạo một Data Patch trong Magento 2 đầu tiên chúng ta phải tạo một file bên trong thư mục Setup/Patch
Tạo file: ViMagento/HelloWorld/Setup/Patch/Data/AddProductAttribute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<?php
namespace ViMagentoHelloWorldSetupPatchData;
use MagentoEavModelEntityAttributeScopedAttributeInterface;
use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
class AddProductAttribute implements DataPatchInterface
{
/**
* ModuleDataSetupInterface
*
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* EavSetupFactory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* AddProductAttribute constructor.
*
* @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’, ‘helloworld’, [
‘type’ => ‘int’,
‘label’ => ‘HelloWorld ViMagento’,
‘input’ => ‘select’,
‘source’ => ‘MagentoEavModelEntityAttributeSourceBoolean’,
‘default’ => 0,
‘global’ => ScopedAttributeInterface::SCOPE_STORE,
‘visible’ => true,
‘used_in_product_listing’ => true,
‘user_defined’ => true,
‘required’ => false,
‘group’ => ‘General’,
‘sort_order’ => 80,
]);
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
|
Vừa rồi tôi dùng Data Patch để thêm một product attribute. Trong đó:
- apply() là function chính, mọi sửa đổi bạn muốn sẽ được viết ở đây
- getDependencies() định nghĩa những phụ thuộc của patch. Ví dụ:
1
2
3
4
5
6
|
public static function getDependencies()
{
return [
SomeVendorSomeModuleSetupPatchDataSomePatch::class
];
}
|
- getAliases() tên định danh cho patch
Kích hoạt Data Patch
Chạy câu lệnh php bin/magento setup:upgrade sau đó truy cập trang admin của bạn vào Catalog > Product > Add Product

Và đây là table patch_list sẽ lưu tất cả các patch đã áp dụng

Lưu ý patch chỉ chạy một lần, Nếu bạn muốn sửa đổi dữ liệu thì bạn có thể tạo một file mới hoặc xóa record vừa tạo trong bảng patch_list và chạy setup:upgrade lại
Kết luận
Sau khi học xong bài này các bạn hãy sử dụng Data Patch để thay cho Setup Script nhé. Các bạn có thể tham khảo bài viết này để biết thêm chi tiết: https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/data-patches.html