magento的EAV模型很是強大且靈活,可是若是不作優化的話,性能會很是低,由於attributes都存放在附表裏,要獲取一個entity的attribute,須要表聯結一次,若是須要獲取多條attributes, 就會產生大量的表聯結,勢必會對數據庫形成壓力,對於訪問量大的內容,好比catalog_product(商品)、商品列表(catalog_category),Magento core team使用了flat table這種策略來應對,簡而言之,就是把須要的attributes的值收集起來,新建一個緩存表,Attribute code做爲列名,attribute value做爲列的值,這樣經過一個SELECT就能夠把不少attributes的值查詢出來。php
緩存表是一把「雙刃劍」,雖然解決了性能問題,卻形成了一些負面影響:
好比:
1. 對系統作了修改,須要從新生成緩存表(reindex),假如商品較多,每每比較耗時
2. 新增attribute時,若是要添加進緩存表比較麻煩,且須要reindex數據庫
新增商品屬性主要使用的是addAttribute()這個方法。數組
1 //file: app\code\core\Mage\Eav\Model\Entity\Setup.php 2 /** 3 * Add attribute to an entity type 4 * 5 * If attribute is system will add to all existing attribute sets 6 * 7 * @param string|integer $entityTypeId 8 * @param string $code 9 * @param array $attr 10 * @return Mage_Eav_Model_Entity_Setup 11 */ 12 public function addAttribute($entityTypeId, $code, array $attr){ ... }
這裏須要注意的是此函數的第三個參數$attr,它是一個數組,裏面規定了新的attribute的各類屬性:緩存
1. 'group' : 商品編輯頁面左側導航欄分組,
2. 'type' : 字段在MySQL中的類型,例如VARCHAR
3. 'label' : 該屬性在商品編輯頁面的label
4. 'input' : 該屬性在商品編輯頁面的Input type
4. 'source' : 若是該屬性是(multi)select等類型,須要定義一個source類爲它提供選項,若是該屬性須要加入緩存表,除了提供選項以外,還須要注意提供該屬性在緩存表中的字段名以及若是更新緩存表,後面還要詳細說明(注1)
5. 'global' : 表明屬性的生效範圍,分別有Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL | SCOPE_WEBSITE | SCOPE_STORE
6. 'required' : 表示是否爲必填項
7. 'user_defined' : 表示是否爲用戶添加(相對與系統原有的而言)
8. 'default' : 該屬性的缺省值
9. 'unique' : 表示該屬性是否具備惟一性
10. 'apply_to' : 表示該屬性對哪一種商品生效,好比'simple,configurable,bundle'表示該屬性對simple和configurable兩種商品生效
11. 'used_in_product_listing' : 表示該屬性會添加到緩存表中app
注1:
假如新增的attribute的input不是‘text’,例如select,則須要提過source類,該source類繼承Mage_Eav_Model_Entity_Attribute_Source_Abstract,須要實現getAllOptions()方法爲attributes提供選項;函數
另外,若是新增的attribute將要添加到緩存表中,首先'used_in_product_listing'應該設置爲1,除此以外,source類還須要實現getFlatColums()方法和getFlatUpdateSelect()方法,
getFlatColums()方法規定attribute映射到緩存表中的字段的各項屬性,包括 type,unsigned,length,nullable等
getFlatUpdateSelect()方法規定緩存表中的值的更新方式,可參考系統中原有的source類來進行設置性能
參考例子:優化
//file: app\code\core\Mage\Eav\Model\Entity\Attribute\Source\Boolean.php class Mage_Eav_Model_Entity_Attribute_Source_Boolean
新增了商品屬性以後,同一個分組中的屬性排序很混亂,可使用addAttributeToGroup()方法從新設置排序:ui
1 //file: app\code\core\Mage\Eav\Model\Entity\Setup.php 2 /** 3 * Add or update attribute to group 4 * 5 * @param int|string $entityType 6 * @param int|string $setId 7 * @param int|string $groupId 8 * @param int|string $attributeId 9 * @param int $sortOrder 10 * @return Mage_Eav_Model_Entity_Setup 11 */ 12 public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId, $sortOrder = null){ ... }
新增了attribute以後,每每須要從新生成緩存表,記得要在在magento中reindex一下spa
1 //file: app\code\core\Mage\Eav\Model\Entity\Setup.php 2 /** 3 * Update Attribute data and Attribute additional data 4 * 5 * @param mixed $entityTypeId 6 * @param mixed $id 7 * @param string $field 8 * @param mixed $value 9 * @param int $sortOrder 10 * @return Mage_Eav_Model_Entity_Setup 11 */ 12 public function updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null){ ... }
注意: 這裏的$field和addAttribute()方法第三個參數$attr所包含的項目不徹底一致,請直接使用eav_attribute表的column名稱。
若是您以爲閱讀本文對您有幫助,歡迎轉載本文,可是轉載文章以後必須在文章頁面明顯位置保留此段聲明,不然保留追究法律責任的權利。
做 者:blog.jpdou.top