1、有兩種類型的屬性能夠擴展magento 功能:php
1:Custom and Entity-attribute-Value (EAV) attributes - Custom屬性是能夠由商家在後臺直接添加的屬性。好比,一個商家可能想添加一個屬性來描述產品,好比產品的形狀或者是體積。商家能夠在Magento Admin 面板添加這些屬性。
Custom 屬性是EAV屬性的一部分。使用EAV屬性的對象一向將值保存在幾個不一樣的Mysql表裏。Customer 和 Catalog 是使用EAV屬性的主要兩個模塊。其它的模塊像C onfigurableProduct , GiftMessage 和Tax 使用的是Catalog的EAV功能。html
2.Extension attributes. Extension attributes 是magento2的新功能。他們被用來擴展功能,並且大多數狀況下數據會比custom屬性複雜。這些屬性不會再magento admin裏出現。sql
2、EAV 和 Custom屬性
CustomAttributesDataInterface定義了用來get 和 set custom屬性的方法,包括getCustomAttributes()
一個模塊一般會有一系列內置屬性。Catalog模塊有一些屬性被定義爲EAV屬性,可是是之內置屬性來處理的,這些屬性包括:
app
在這種狀況下,當調用getCustomAttributes()方法的時候,列表裏的屬性將不會被返回。frontend
Customer模塊沒有對它對EAV屬性作特殊處理,因此,getCustomAttributes()
將返回全部的EAV屬性。
ui
Both the save()
and getResource()
methods for Magento\Framework\Model\AbstractModel
have been marked as @deprecated
since 2.1 and should no longer be used.this
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 |
<?php namespace Magento\Customer\Setup\Patch\Data; use Magento\Customer\Model\Customer; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Framework\App\ResourceConnection; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchVersionInterface; class AddCustomerExampleAttribute implements DataPatchInterface { private $moduleDataSetup; private $customerSetupFactory; public function __construct( ModuleDataSetupInterface $moduleDataSetup, CustomerSetupFactory $customerSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->customerSetupFactory = $customerSetupFactory; } public function apply() { $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerSetup->addAttribute(Customer::ENTITY, 'attribute_code', [ // Attribute parameters ]); } /** * {@inheritdoc} */ public static function getDependencies() { return [ UpdateIdentifierCustomerAttributesVisibility::class, ]; } public function getAliases() { return []; } } |
2、Extension attributes
使用ExtensibleDataInterface來實現extension屬性。在你的代碼裏,你必須聲明
getExtensionAttributes()和setExtensionAttributes(*ExtensionInterface param)
.
很大程度上,你須要擴展在Api/Data directory 定義好的模塊。spa
聲明Extension attributes
你必須建立一個 <Module>/etc/extension_attributes.xml文件來定義一個模塊的擴展屬性:
.net
<config> <extension_attributes for="Path\To\Interface"> <attribute code="name_of_attribute" type="datatype"> <resources> <resource ref="permission"/> </resources> <join reference_table="" reference_field="" join_on_field=""> <field>fieldname</field> </join> </attribute> </extension_attributes> </config>
搜索extension 屬性:
系統使用Join把擴展屬性添加到collection以供篩選。extension_attribute.xml定義了那個表/列的那個字段被用來做爲搜索的源。調試
在下面的例子中,Magento\CatalogInventory\Api\Data\StockItemInterface類型的屬性stock_item,被添加帶來Magento\Catalog\Api\Data\ProductInterface的搜索結果
當getList()方法被調用的時候會返回ProductInterfaces的列表,這時,因爲Product
’s entity_Id
和cataloginventory_stock_item.product_id已被關聯起來,stock_item將被賦予cataloginventory_stock_item表裏的qty字段。
extension字段訪問權限
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface"> <attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface"> <resources> <resource ref="Magento_CatalogInventory::cataloginventory"/> </resources> </attribute> </extension_attributes> </config>
這裏表示,只有擁有Magento_CatalogInventory::cataloginventory權限的人才能訪問
stock_item.
Extension 接口
若是extension屬性爲空,extension接口會爲空。
interface CustomerExtensionInterface extends \Magento\Framework\Api\ExtensionAttributesInterface { }
可是,若是一個以下的擴展被添加,接口就不會爲空:
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface"> <attribute code="attributeName" type="Magento\Some\Type[]" /> </extension_attributes>
調試EAV屬性
若是你再使用setup:upgrade的時候遇到問題,請去確認下__construct使用了EavSetupFactory而不是EavSetup。你不能在擴展的代碼裏直接注入EavSetup。檢查你的定製模塊或者購買的擴展,修改完以後應該就能正常部署了。
添加產品屬性時的配置項
下面的列表是Magento\Eav\Setup\EavSetup::addAttribute的參考項,它提供了建立產品屬性時的可選的項,列出了每一個可選項的鍵值,描述,默認值。
Key | Description | Default Value |
---|---|---|
apply_to | Catalog EAV Attribute apply_to | |
attribute_model | EAV Attribute attribute_model | |
attribute_set | Name of the attribute set the new attribute will be assigned to. Works in combination with group or empty user_defined | |
backend | EAV Attribute backend_model | |
comparable | Catalog EAV Attribute is_comparable | 0 |
default | EAV Attribute default_value | |
filterable_in_search | Catalog EAV Attribute is_filterable_in_search | 0 |
filterable | Catalog EAV Attribute is_filterable | 0 |
frontend_class | EAV Attribute frontend_class | |
frontend | EAV Attribute frontend_model | |
global | Catalog EAV Attribute is_global field | 1 |
group | Attribute group name or ID | |
input_renderer | Catalog EAV Attribute frontend_input_renderer | |
input | EAV Attribute frontend_input | text |
is_filterable_in_grid | Catalog EAV Attribute is_filterable_in_grid | 0 |
is_html_allowed_on_front | Catalog EAV Attribute is_html_allowed_on_front | 0 |
is_used_in_grid | Catalog EAV Attribute is_used_in_grid field | 0 |
is_visible_in_grid | Catalog EAV Attribute is_visible_in_grid field | 0 |
label | EAV Attribute frontend_label | |
note | EAV Attribute note | |
option | EAV Attribute Option values | |
position | Catalog EAV Attribute position | 0 |
required | EAV Attribute is_required | 1 |
searchable | Catalog EAV Attribute is_searchable | 0 |
sort_order | EAV Entity Attribute sort_order | |
source | EAV Attribute source_model | |
table | EAV Attribute backend_table | |
type | EAV Attribute backend_type | varchar |
unique | EAV Attribute is_unique | 0 |
used_for_promo_rules | Catalog EAV Attribute is_used_for_promo_rules | 0 |
used_for_sort_by | Catalog EAV Attribute used_for_sort_by | 0 |
used_in_product_listing | Catalog EAV Attribute used_in_product_listing | 0 |
user_defined | EAV Attribute is_user_defined | 0 |
visible_in_advanced_search | Catalog EAV Attribute is_visible_in_advanced_search | 0 |
visible_on_front | Catalog EAV Attribute is_visible_on_front | 0 |
visible | Catalog EAV Attribute is_visible | 1 |
wysiwyg_enabled | Catalog EAV Attribute is_wysiwyg_enabled | 0 |