今天我将要讲述Magento 2中的EAV模型,Magento 2的数据库使用的技术是一个 以实体-属性-值得模型的一种数据模型简称:EAV模型
实体-属性-值(Entity-Attribute-Value)模型是一种数据模型,用于描述期望属性数量非常多的实体,但实际上,实体中使用的属性数量很少。
实体=Entity :存储有关所存储数据类型的信息。在Magento的例子中customer product, category等等
属性=Attribute :是每个实体的单独属性(名称、权重、颜色、尺寸、电子邮件地址)。
值=Value :是给定实体和属性的值。
EAV模式的优点如下:灵活多变的数据结构(可以在不更改数据库模式的情况下更改属性的数量)。
当为给定实体添加新属性时,我们有可能在其他实体中使用它。
快速实现。
EAV实体的数据库模式:eav_entity – (E)实体表。
eav_entity_attribute (A) Attrubute表
eav_entity_{type} (V) -值表。{type} – datetime,decimals,int, text和varchar。
图表与eav_*表:
Magento 2中有哪些EAV实体(E):
实体列表可以在eav_entity_type表中找到:customer_entity
customer_address_entity
catalog_category_entity
catalog_product_entity
以上这些都是实体对象,实体是指数据项 ,分别是类别,产品,客户和客户地址的实体表。
在Magento 2中有哪些EAV实体类型:eav_entity_int
eav_entity_varchar
eav_entity_text
eav_entity_decimal
eav_entity_datetime
实体catalog_product在Magento 2中
为了理解Magento 2中的EAV模型,让我们考虑“catalog_product”示例、用于创建属性的表的类型、属性集中属性的函数和属性组中的属性。此外,我们还将更仔细地查看用于分配带有属性的产品的表。
Magento 2中EAV属性(A):
属性是指实体的不同属性。在Magento中,与实体相关的属性存储在单个表中:eav_attribute但由字段:entity_type_id区分。我们可以使用以下SQL查询轻松找到与实体相关的所有属性(例如产品):
1
2
3
4
5
6
7
8
9
10
11
12 SELECT
attribute_code,
attribute_id,
backend_type
FROM
eav_attribute
WHEREentity_type_id=
(SELECT
entity_type_id
FROM
eav_entity_type
WHEREentity_type_code='catalog_product')
输出的结果如下图
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
81
82
83
84
85
86
87
88 +----------------------------+--------------+--------------+
|attribute_code|attribute_id|backend_type|
+----------------------------+--------------+--------------+
|activation_information|496|text|
|color|272|int|
|color_code|950|varchar|
|computer_manufacturers|510|int|
|contrast_ratio|875|int|
|cost|100|decimal|
|country_orgin|507|text|
|cpu_speed|877|int|
|created_at|930|static|
|custom_design|571|varchar|
|custom_design_from|572|datetime|
|custom_design_to|573|datetime|
|custom_layout_update|531|text|
|description|97|text|
|dimension|494|text|
|enable_googlecheckout|903|int|
|finish|509|text|
|gallery|271|varchar|
|gender|501|int|
|gift_message_available|562|varchar|
|harddrive_speed|878|varchar|
|hardrive|499|text|
|has_options|838|static|
|image|106|varchar|
|image_label|879|varchar|
|in_depth|492|text|
|is_imported|949|int|
|is_recurring|933|int|
|links_exist|947|int|
|links_purchased_separately|904|int|
|links_title|906|varchar|
|manufacturer|102|int|
|max_resolution|873|varchar|
|media_gallery|703|varchar|
|megapixels|513|int|
|memory|498|text|
|meta_description|105|varchar|
|meta_keyword|104|text|
|meta_title|103|varchar|
|minimal_price|503|decimal|
|model|495|text|
|name|96|varchar|
|news_from_date|704|datetime|
|news_to_date|705|datetime|
|old_id|110|int|
|options_container|836|varchar|
|package_id|951|int|
|page_layout|929|varchar|
|price|99|decimal|
|price_type|859|int|
|price_view|862|int|
|processor|497|text|
|ram_size|874|varchar|
|recurring_profile|934|text|
|required_options|837|static|
|response_time|876|varchar|
|room|508|int|
|samples_title|905|varchar|
|screensize|500|text|
|shape|476|text|
|shipment_type|863|int|
|shipping_qty|952|int|
|shirt_size|525|int|
|shoe_size|502|int|
|shoe_type|107|int|
|short_description|506|text|
|sku|98|static|
|sku_type|860|int|
|small_image|109|varchar|
|small_image_label|880|varchar|
|special_from_date|568|datetime|
|special_price|567|decimal|
|special_to_date|569|datetime|
|status|273|int|
|tax_class_id|274|int|
|thumbnail|493|varchar|
|thumbnail_label|881|varchar|
|tier_price|270|decimal|
|updated_at|931|static|
|url_key|481|varchar|
|url_path|570|varchar|
|visibility|526|int|
|weight|101|decimal|
|weight_type|861|int|
+----------------------------+--------------+--------------+
您只需使用entity_type_code的以下值即可找到其他实体的属性:catalog_category
customer
customer_address
您可以从表中获取entity_type_code的相关值:eav_entity_type
Magento 2中EAV值(V):
值是指实体属性的实际值。在Magento中,实体的属性值(例如:product)存储在catalog_product_entity_ {backend_type}表中。
其中{backend_type}引用table:eav_attribute的field:backend_type(值’static’除外)的值。
以下SQL用于查找与产品实体相关的所有backend类型:
1
2
3
4
5
6
7
8
9
10 SELECTDISTINCT
backend_type
FROM
eav_attribute
WHEREentity_type_id=
(SELECT
entity_type_id
FROM
eav_entity_type
WHEREentity_type_code='catalog_product')
输出如下
1
2
3
4
5
6
7
8
9
10 +--------------+
|backend_type|
+--------------+
|text|
|int|
|varchar|
|decimal|
|static|
|datetime|
+--------------+
catalog_product_entity_text
catalog_product_entity_int
catalog_product_entity_varchar
catalog_product_entity_decimal
catalog_product_entity_datetime
用于存储相关backend_type的属性的值。
你一定想知道backend_type = static。对于具有静态backend_type的属性,值将直接存储为实体表中的列。例如,sku是backend_type = static的属性,其值存储在实体表本身中:catalog_product_entity在field:sku下。
这就是与Magento有关的EAV的定义。希望现在您能够根据backend_type区分哪些属性值(在哪个表中)。接下我将给大家举例子说明。希望对你了解magento有所帮助。
例如:在Magento 2中为产品添加EAV属性
Magento有两种类型的属性可用于提供额外的功能,即Eav属性和扩展属性。在这里,我将讨论eav属性,并展示如何在Magento 2中为产品添加eav属性。
入口属性值(eav)属性是商家从管理面板中添加的描述产品的属性。使用自定义或eav属性描述形状、大小等属性。
步骤1:声明EAV安装工厂
1
2
3
4
5
6
7
8
9
10
11
12
13
14 /**
* @var EavSetupFactory
*/
protected$eavSetupFactory;
/**
* UpgradeData constructor
*
* @param EavSetupFactory $eavSetupFactory
*/
publicfunction__construct(EavSetupFactory$eavSetupFactory)
{
$this->eavSetupFactory=$eavSetupFactory;
}
步骤2:添加属性
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 /** @var EavSetup $eavSetup */
$eavSetup=$this->eavSetupFactory->create(['setup'=>$setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'is_featured',
[
'group'=>'General',
'type'=>'int',
'backend'=>'',
'frontend'=>'',
'label'=>'Is Featured',
'input'=>'boolean',
'class'=>'',
'source'=>'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global'=>\Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible'=>true,
'required'=>false,
'user_defined'=>false,
'default'=>'1',
'searchable'=>false,
'filterable'=>false,
'comparable'=>false,
'visible_on_front'=>false,
'used_in_product_listing'=>false,
'unique'=>false,
'apply_to'=>''
]
);
is_featured:属性code
group:组名称属性,将显示在后台
type:数据类型保存在数据库
global:属性的范围(store, website or global)
visible_on_frontend:允许在前端显示属性的true或false,或no
apply_to:产品类型您想要添加的属性
步骤3:删除产品的EAV属性
1
2
3 $entityTypeId=4;// Find these in the eav_entity_type table
$eavSetup->removeAttribute($entityTypeId,'is_featured');
按照上面的步骤,轻松地将EAV属性添加到Magento 2中的产品中!我试图提出一个简单的方法来完成这项任务,希望它能有所帮助。如在执行上述步骤时遇到任何问题,请在下方提出意见。