日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java eav_动态自定义字段属性–Magento的EAV模型 | 学步园

發布時間:2025/3/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java eav_动态自定义字段属性–Magento的EAV模型 | 学步园 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

EAV : Entity - Attribute - Value 的縮寫,是數據庫模型的一種,使用eav建模的好處是可以動態為數據模型增加或移除屬性。

1. 問題提出:

假設需要定義一個實體Customer的信息,通常我們只要定義一個表為customer,并定義相應的屬性即可。倘若某天需要為customer增加一個新的屬性如“畢業學校”,那么就需要更改表的結構。

如果使用EAV模型則不必改變表結構。

2. Magento的EAV模型定義:

在Magento中,EAV模型相關的表定義有:

Java代碼

eav_attribute

eav_attribute_group

eav_attribute_option

eav_attribute_option_value

eav_attribute_set

eav_entity

eav_entity_attribute

eav_entity_datetime

eav_entity_decimal

eav_entity_int

eav_entity_store

eav_entity_text

eav_entity_type

eav_entity_varchar

eav_attribute

eav_attribute_group

eav_attribute_option

eav_attribute_option_value

eav_attribute_set

eav_entity

eav_entity_attribute

eav_entity_datetime

eav_entity_decimal

eav_entity_int

eav_entity_store

eav_entity_text

eav_entity_type

eav_entity_varchar

現在讓我來觀察最重要的三張表

eav_entity_type,eav_entity_attribute,eav_attribute

1) eav_entity_type表用來定義實體的基本信息。

Sql代碼

mysql>select*fromeav_entity_typewhereentity_type_id=1;

+----------------+------------------+-------------------+-----------------+-----------------+

|?entity_type_id?|?entity_type_code?|?entity_model??????|?attribute_model?|?entity_table????|?+----------------+------------------+-------------------+-----------------+-----------------+

|??????????????1?|?customer?????????|?customer/customer?|?????????????????|?customer/entity?|

+----------------+------------------+-------------------+-----------------+-----------------+

mysql> select * from eav_entity_type where entity_type_id=1;

+----------------+------------------+-------------------+-----------------+-----------------+

| entity_type_id | entity_type_code | entity_model | attribute_model | entity_table | +----------------+------------------+-------------------+-----------------+-----------------+

| 1 | customer | customer/customer | | customer/entity |

+----------------+------------------+-------------------+-----------------+-----------------+

2). eav_entity_attribute表用來定義實體customer模型包含哪些屬性

Sql代碼

mysql>select*fromeav_entity_attributewhereentity_type_id='1';

+---------------------+----------------+------------------+--------------------+--------------+

|?entity_attribute_id?|?entity_type_id?|?attribute_set_id?|?attribute_group_id?|?attribute_id?|

+---------------------+----------------+------------------+--------------------+--------------+

|???????????????????1?|??????????????1?|????????????????1?|??????????????????1?|????????????1?|

|???????????????????2?|??????????????1?|????????????????1?|??????????????????1?|????????????2?|

|???????????????????3?|??????????????1?|????????????????1?|??????????????????1?|????????????3?|

|???????????????????4?|??????????????1?|????????????????1?|??????????????????1?|????????????4?|

|???????????????????5?|??????????????1?|????????????????1?|??????????????????1?|????????????5?|

|???????????????????6?|??????????????1?|????????????????1?|??????????????????1?|????????????6?|

|???????????????????7?|??????????????1?|????????????????1?|??????????????????1?|????????????7?|

|???????????????????8?|??????????????1?|????????????????1?|??????????????????1?|????????????8?|

|???????????????????9?|??????????????1?|????????????????1?|??????????????????1?|????????????9?|

|??????????????????10?|??????????????1?|????????????????1?|??????????????????1?|???????????10?|

|??????????????????11?|??????????????1?|????????????????1?|??????????????????1?|???????????11?|

|??????????????????12?|??????????????1?|????????????????1?|??????????????????1?|???????????12?|

|??????????????????13?|??????????????1?|????????????????1?|??????????????????1?|???????????13?|

|??????????????????14?|??????????????1?|????????????????1?|??????????????????1?|???????????14?|

|??????????????????15?|??????????????1?|????????????????1?|??????????????????1?|???????????15?|

|??????????????????16?|??????????????1?|????????????????1?|??????????????????1?|???????????16?|

+---------------------+----------------+------------------+--------------------+--------------+

mysql> select * from eav_entity_attribute where entity_type_id='1';

+---------------------+----------------+------------------+--------------------+--------------+

| entity_attribute_id | entity_type_id | attribute_set_id | attribute_group_id | attribute_id |

+---------------------+----------------+------------------+--------------------+--------------+

| 1 | 1 | 1 | 1 | 1 |

| 2 | 1 | 1 | 1 | 2 |

| 3 | 1 | 1 | 1 | 3 |

| 4 | 1 | 1 | 1 | 4 |

| 5 | 1 | 1 | 1 | 5 |

| 6 | 1 | 1 | 1 | 6 |

| 7 | 1 | 1 | 1 | 7 |

| 8 | 1 | 1 | 1 | 8 |

| 9 | 1 | 1 | 1 | 9 |

| 10 | 1 | 1 | 1 | 10 |

| 11 | 1 | 1 | 1 | 11 |

| 12 | 1 | 1 | 1 | 12 |

| 13 | 1 | 1 | 1 | 13 |

| 14 | 1 | 1 | 1 | 14 |

| 15 | 1 | 1 | 1 | 15 |

| 16 | 1 | 1 | 1 | 16 |

+---------------------+----------------+------------------+--------------------+--------------+

3), 由上表推出customer實體包含16個屬性,下面的語句查看每個屬性的具體定義

Sql代碼

mysql>select*fromeav_attributewhereattribute_id<=16andattribute_id>=1?;

+--------------+----------------+------------------+--------------+

|?attribute_id?|?entity_type_id?|?attribute_code???|?backend_type?|

+--------------+----------------+------------------+--------------+

|????????????1?|??????????????1?|?website_id???????|?static|

|????????????2?|??????????????1?|?store_id?????????|?static|

|????????????3?|??????????????1?|?created_in???????|?varchar|

|????????????4?|??????????????1?|?prefix???????????|?varchar|

|????????????5?|??????????????1?|?firstname????????|?varchar|

|????????????6?|??????????????1?|?middlename???????|?varchar|

|????????????7?|??????????????1?|?lastname?????????|?varchar|

|????????????8?|??????????????1?|?suffix???????????|?varchar|

|????????????9?|??????????????1?|?email????????????|?static|

|???????????10?|??????????????1?|?group_id?????????|?static|

|???????????11?|??????????????1?|?dob??????????????|?datetime?????|

|???????????12?|??????????????1?|?password_hash????|?varchar|

|???????????13?|??????????????1?|?default_billing??|?int|

|???????????14?|??????????????1?|?default_shipping?|?int|

|???????????15?|??????????????1?|?taxvat???????????|?varchar|

|???????????16?|??????????????1?|?confirmation?????|?varchar|

+--------------+----------------+------------------+--------------+

mysql> select * from eav_attribute where attribute_id<=16 and attribute_id>=1 ;

+--------------+----------------+------------------+--------------+

| attribute_id | entity_type_id | attribute_code | backend_type |

+--------------+----------------+------------------+--------------+

| 1 | 1 | website_id | static |

| 2 | 1 | store_id | static |

| 3 | 1 | created_in | varchar |

| 4 | 1 | prefix | varchar |

| 5 | 1 | firstname | varchar |

| 6 | 1 | middlename | varchar |

| 7 | 1 | lastname | varchar |

| 8 | 1 | suffix | varchar |

| 9 | 1 | email | static |

| 10 | 1 | group_id | static |

| 11 | 1 | dob | datetime |

| 12 | 1 | password_hash | varchar |

| 13 | 1 | default_billing | int |

| 14 | 1 | default_shipping | int |

| 15 | 1 | taxvat | varchar |

| 16 | 1 | confirmation | varchar |

+--------------+----------------+------------------+--------------+

所以,使用上述的模型,一旦有CUSTOMER屬性定義的添加和刪除,只需要增加或刪除 eav_entity_attribute的記錄即可

3. 使用EAV模型

現在,在Magento系統注冊一個新的用戶,看看實例數據如何存放在數據庫

Sql代碼

mysql>select*fromcustomer_entity????????????;

+-----------+----------------+------------------+------------+--------------------+

|?entity_id?|?entity_type_id?|?attribute_set_id?|?website_id?|?email??????????????|

+-----------+----------------+------------------+------------+--------------------+

|?????????1?|??????????????1?|????????????????0?|??????????1?|?koda.guo@gmail.com?|

+-----------+----------------+------------------+------------+--------------------+

mysql>?select*fromcustomer_entity_varchar????;

+----------+----------------+--------------+-----------+-------------------------------------+

|?value_id?|?entity_type_id?|?attribute_id?|?entity_id?|?value???????????????????????????????|

+----------+----------------+--------------+-----------+-------------------------------------+

|????????1?|??????????????1?|????????????5?|?????????1?|?Koda????????????????????????????????|

|????????2?|??????????????1?|????????????7?|?????????1?|?Guo?????????????????????????????????|

|????????4?|??????????????1?|????????????3?|?????????1?|?DefaultStoreView|

|????????5?|??????????????1?|???????????12?|?????????1?|?2256e441b74ab3454a41c821f5de1e9d:9s?|

+----------+----------------+--------------+-----------+-------------------------------------+

mysql> select * from customer_entity ;

+-----------+----------------+------------------+------------+--------------------+

| entity_id | entity_type_id | attribute_set_id | website_id | email |

+-----------+----------------+------------------+------------+--------------------+

| 1 | 1 | 0 | 1 | koda.guo@gmail.com |

+-----------+----------------+------------------+------------+--------------------+

mysql> select * from customer_entity_varchar ;

+----------+----------------+--------------+-----------+-------------------------------------+

| value_id | entity_type_id | attribute_id | entity_id | value |

+----------+----------------+--------------+-----------+-------------------------------------+

| 1 | 1 | 5 | 1 | Koda |

| 2 | 1 | 7 | 1 | Guo |

| 4 | 1 | 3 | 1 | Default Store View |

| 5 | 1 | 12 | 1 | 2256e441b74ab3454a41c821f5de1e9d:9s |

+----------+----------------+--------------+-----------+-------------------------------------+

從上表看到customer_entity 和customer_entity_varchar用來存放相應屬性的實際輸入值。如:

Koda, Guo分別屬性編號5,7即firstname和lastname實際值

和customer實體定義相對應的實例存放的相關表包括:

Java代碼

customer_entity

customer_entity_datetime

customer_entity_decimal

customer_entity_int

customer_entity_text

customer_entity_varchar

customer_entity

customer_entity_datetime

customer_entity_decimal

customer_entity_int

customer_entity_text

customer_entity_varchar

總結

了解Magento的EAV模型結構是擴展Magento必須的知識。 其意義在于,我們常常需要擴展Magento某些實體的屬性,或者創建自定義的eav模型實例。希望本文對你有所啟示。

總結

以上是生活随笔為你收集整理的java eav_动态自定义字段属性–Magento的EAV模型 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。