天天看點

Magento 資料庫EVA

什麼是eva

eva代表實體,屬性和值。讓我們看看它的每一部分,試着更好的了解它。

實體

實體代表等産品,種類,客戶和訂單magento的資料項。每個實體(産品,類别等)都會有它的資料庫中的實體記錄。

屬性(attribute_code)

該屬性表示資料項屬于一個實體。例如,産品實體,如名稱,價格,地位和更多的屬性。

該值是最簡單的了解,因為它是簡單地連結到一個屬性的值。

為了更好地了解這一點,讓我們考慮産品的實體。每個産品的實體将有一個屬性,一個是屬性名稱系列。每一種産品将有一個屬性名稱值(和所有其他屬性)。

eva是怎麼工作的 ?

讓我們用一個産品表作為例子。比起将所有的資料存取在一個表格中,magneto将資訊分開存儲在多張表格中。

在這種層次結構的頂部表是 catalog_product_entity. 如果你在phpmyadmin看看這個表,你會看到它包含了産品的簡單基本資料,也沒有出現任何有用的資訊,包括sku!幸運的是,使用此表是有可能建立從屬性和值表的完整的産品記錄。

要開始建構一個完整的産品記錄,您将需要開始加入到産品實體的屬性表。你這樣做之前,看一下名為eav_attribute的表。eav_attribute是存儲屬性的主要表,用于存儲所有不同的實體的屬性(産品,客戶,訂單,分類等)。magento的怎麼知道哪一個屬性屬于一個産品,或者屬于一個類别?由于通常是與magento情況下,每一個實體 (産品,客戶,訂單,分類等) 都有一個 entity_type_id.要了解這一點,回去 catalog_product_entity 查找entity_type_id 這個字段.在該表中每個記錄的值應該是4,因為這已被列為指定産品entity_type_id。如果你看看 catalog_category_entity你将看到另一個不同的 entity_type_id。使用這個值和屬性代碼,它可以為一個産品或任何實體加載屬性。找到attribute_code對應的attribute_id和backend_type,如果backend_type不為空,就是用的catalog_category_entity_{backend_type}表,為空就是catalog_category_entity。通過attribute_id,catalog_category_entity中的entity_id可以找到莫一條紀錄的值

為什麼要使用eva?

eav 是因為它比一般規範化的資料庫結構擴充性更好。開發人員無需修改資料庫結構的核心就可以為任何實體(産品,品類,客戶,訂單等)添加屬性。當建立一個自定義屬性的時候,不用為儲存這個屬性去建立邏輯結構,因為它已經被建立到資料庫模型中;隻要資料集和屬性已經建立,該模型将被儲存!

eav的缺點是什麼?

eav一個主要缺點是它的速度。因為實體資料如此支離破碎,創造了整個實體記錄需要大量的表聯接。幸運的是,在瓦瑞恩團隊有一個很好的緩存系統實施,允許開發者緩存不經常改變的資料。

eav的另一個問題是它學習有曲線性, 這樣很多初級開發人員在了解到它的的簡單之前就已經放棄了。雖然沒有此快速修複,希望這篇文章将有助于人們開始解決這個問題。

結論

實體,屬性,價值是一個偉大的資料庫結構,是magento成功的一個關鍵部分,開發人員了解如何運作是非常重要的。這方面的知識也有很多應用,我相信如果你的magento工作時間足夠長,你會碰到一些!

Magento 資料庫EVA

select `e`.entity_id as customer_id,`e`.email,_table_billing_name.value as realname,_table_billing_addree.value as address,  

_table_billing_province.value as province,_table_billing_city.value as city,_table_billing_postcode.value as postcode  

from `customer_entity` as `e`   

left join `customer_entity_int` as `_table_default_billing` on (`_table_default_billing`.`entity_id` = `e`.`entity_id`) and (`_table_default_billing`.`attribute_id` = '7')  

left join `customer_address_entity_varchar` as `_table_billing_name` on (`_table_billing_name`.`entity_id` = `_table_default_billing`.`value`) and (`_table_billing_name`.`attribute_id` = '9')   

left join `customer_address_entity_varchar` as `_table_billing_postcode` on (`_table_billing_postcode`.`entity_id` = `_table_default_billing`.`value`) and (`_table_billing_postcode`.`attribute_id` = '14')   

left join `customer_address_entity_varchar` as `_table_billing_province` on (`_table_billing_province`.`entity_id` = `_table_default_billing`.`value`) and (`_table_billing_province`.`attribute_id` = '12')   

left join `customer_address_entity_varchar` as `_table_billing_city` on (`_table_billing_city`.`entity_id` = `_table_default_billing`.`value`) and (`_table_billing_city`.`attribute_id` = '15')   

left join `customer_address_entity_text` as `_table_billing_addree` on (`_table_billing_addree`.`entity_id` = `_table_default_billing`.`value`) and (`_table_billing_addree`.`attribute_id` = '16')