Khi tạo một product, customer…attribute bạn có bao giờ tự hỏi rằng các thuộc tính của attribute bạn vừa thêm vào được lưu ở đâu chưa. Trong bài viết này mình sẽ giới thiệu với các bạn về cách Magento sẽ lưu các attribute vào database như thế nào cũng như cách quản lý eav attribute.
Ví dụ về tạo Eav Attribute (product attribute)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute('catalog_product', 'my_attribute', [ 'type' => 'int', 'label' => 'My Attribute', 'input' => 'text', 'default' => 0, 'global' => ScopedAttributeInterface::<em>SCOPE_STORE</em>, 'visible' => true, 'used_in_product_listing' => true, 'user_defined' => true, 'required' => false, 'group' => 'General', 'sort_order' => 80, ]); |
Phía trên là một ví dụ về cách thêm product attribute đơn giản trong Magento 2. Nhưng sau khi attribute được tạo thì chúng lại được lưu vào bảng eav_attribute
với tên khác với những gì chúng ta đã trình bày trong đoạn code trên. Cụ thể là:

Như các bạn có thể thấy các thuộc tính khi chúng ta tạo attribute và các cột trong bảng eav_attribute
không hoàn toàn giống nhau. Vậy làm thế nào để Magento có thể lưu được các thuộc tính này một cách chính xác. Câu trả lời là do Magento đã map tên các thuộc tính đầu vào tương ứng với các cột trong bảng eav_attribute
.
Các bạn có thể xem trong code core của Magento:
Interface: vendor/magento/module-eav/Model/Entity/Setup/PropertyMapperInterface.php
Eav: vendor/magento/module-eav/Model/Entity/Setup/PropertyMapper.php
Product: vendor/magento/module-catalog/Model/ResourceModel/Setup/PropertyMapper.php
Customer: vendor/magento/module-customer/Model/ResourceModel/Setup/PropertyMapper.php

EAV Attribute Mapping
vendor/magento/module-eav/Model/Entity/Setup/PropertyMapper.php
phương thức map sẽ map các thông tin thuộc tính đầu vào tương ứng với các cột trong bảng eav_attribute
. Đây là các thuộc tính chung cơ bản được sử dụng cho hầu hết các EAV Entity.
Thuộc tính trong Setup Script hoặc Data Patch | Thuộc tính trong Database | Giá trị mặc định của thuộc tính |
attribute_model | attribute_model | null |
backend | backend_model | null |
type | backend_type | varchar |
table | backend_table | null |
frontend | frontend_model | null |
input | frontend_input | text |
label | frontend_label | null |
frontend_class | frontend_class | null |
source | source_model | null |
required | is_required | 1 |
user_defined | is_user_defined | 0 |
default | default_value | null |
unique | is_unique | 0 |
note | note | null |
global | is_global | \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL |
Ví dụ về mapping product: vendor/magento/module-catalog/Model/ResourceModel/Setup/PropertyMapper.php
Thuộc tính trong Setup Script hoặc Data Patch | Thuộc tính trong Database | Giá trị mặc định của thuộc tính |
input_renderer | frontend_input_renderer | null |
global | is_global | \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL |
visible | is_visible | 1 |
searchable | is_searchable | 0 |
filterable | is_filterable | 0 |
comparable | is_comparable | 0 |
visible_on_front | is_visible_on_front | 0 |
wysiwyg_enabled | is_wysiwyg_enabled | 0 |
is_html_allowed_on_front | is_html_allowed_on_front | 0 |
visible_in_advanced_search | is_visible_in_advanced_search | 0 |
filterable_in_search | is_filterable_in_search | 0 |
used_in_product_listing | used_in_product_listing | 0 |
used_for_sort_by | used_for_sort_by | 0 |
apply_to | apply_to | null |
position | position | null |
used_for_promo_rules | is_used_for_promo_rules | 0 |
is_used_in_grid | is_used_in_grid | 0 |
is_visible_in_grid | is_visible_in_grid | 0 |
is_filterable_in_grid | is_filterable_in_grid | 0 |
Các thuộc tính riêng của catalog, customer được lưu ở đâu
Bảng eav_attribute
chỉ lưu các thông tin chung của các EAV Entity (product, customer, customer address…). Nhưng mỗi Entity lại yêu cầu các dữ liệu khác nhau cho các thuộc tính của chúng. Ví dụ: thuộc tính “Used in layered navigation” chỉ hoạt động đối với products, còn các entity khác như customer… thì lại không.
Vì thế các thuộc tính chỉ được sử dụng cho catalog entity (product, category) sẽ được lưu vào một bảng riêng là catalog_eav_attribute
. Mỗi dòng trong bảng catalog_eav_attribute
sẽ được liên kết với dòng tương ứng trong bảng eav_attribute
. Chúng được liên kết với nhau thông qua thuộc tính attribute_id
.
Tương tự như thế đối với customer ta sẽ có bảng customer_eav_attribute
để lưu các thuộc tính riêng cho customer attribute.
Bài viết tham khảo: https://belvg.com/blog/how-to-manage-eav-attributes-including-interface-source-backend-structure-in-magento-2.html