java eav模式_Magento 2中EAV模型的理解

今天我将要讲述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_*表:

d97cad879705c262a462acdd8f69822a.png

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中的产品中!我试图提出一个简单的方法来完成这项任务,希望它能有所帮助。如在执行上述步骤时遇到任何问题,请在下方提出意见。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 首先,让我们来了解一下什么是 EAV 模型EAV 模型是一种数据建模的方法,它将所有的数据存储在一个三元组 (实体、属性、值) 。其,实体表示存储的对象,属性表示实体的某个特征,值则表示该属性的具体取值。这种模型可以用于存储非结构化数据和动态的数据,但是在查询和维护方面会存在一些挑战。 接下来,让我们来看一下如何实现简单的转存。Java 提供了许多用于文件操作的类和方法,其最常用的是 java.io 包的类。我们可以使用 FileInputStream 和 FileOutputStream 类来实现文件的读写操作。 例如,我们可以通过以下代码将源文件的内容复制到目标文件: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("source.txt"); FileOutputStream out = new FileOutputStream("target.txt"); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); System.out.println("File copied successfully!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码,我们首先创建了一个 FileInputStream 对象来读取源文件,然后创建了一个 FileOutputStream 对象来写入目标文件。接着,我们使用一个字节数组作为缓冲区,每次读取 1024 个字节,并将其写入目标文件。最后,我们关闭了输入输出流,完成了文件复制操作。 希望这个简单的例子能够帮助你入门 Java IO 操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值