EAV和扩展属性
Magento提供了两种类型的属性集成商可以使用扩展中提供的功能属性:
- 
              
自定义和EAV (Entity-Attribute-Value) 属性.自定义属性是那些商家添加的属性。例如,商家可能需要添加属性来描述产品
自定义属性EAV属性的子集。
 
EAV 和 自定义属性
CustomAttributesDataInterface 定义获取和设置自定义属性的方法,包括 getCustomAttributes().
模块有一组内置的属性,总是可用的。Catalog 模块具有几个属性,定义为EAV属性,但作为内置属性。这些属性包括:
- attribute_set_id
 - created_at
 - group_price
 - media_gallery
 - name
 - price
 - sku
 - status
 - store_id
 - tier_price
 - type_id
 - updated_at
 - visibility
 - weight
 
扩展属性
使用ExtensibleDataInterface 实现扩展属性. 在代码中,必须定义 getExtensionAttributes() 和 setExtensionAttributes(*ExtensionInterface param).方法
public function getExtensionAttributes();
将扩展接口定义在 Api/Data 目录,在magento 2 模块.
声明扩展属性
你必须在模块下面创建一个 <Module>/etc/extension_attributes.xml 文件定义扩展属性:
<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>
          | 关键字 | 描述 | 示例 | 
|---|---|---|
for  | 
                具有处理扩展名的命名空间的完全限定类型名称。该值必须是一个类型,实现了` extensibledatainterface `。接口可以在不同的模块。  | 
                Magento\Quote\Api\Data\TotalsInterface | 
              
code  | 
                属性名称。属性名应为大写(每个单词的首字母应小写,每个单词由下划线分隔)。  | 
                gift_cards_amount_used | 
              
type  | 
                数据类型。这可以是简单的数据类型,如字符串或整数,或复杂类型,例如接口。  | 
                float  | 
              
ref  | 
                可选。使用指定权限限制对扩展属性的访问。  | 
                Magento_CatalogInventory::cataloginventory | 
              
reference_table  | 
                参与联接操作的表。查看 搜索扩展属性 .  | 
                admin_user | 
              
reference_field  | 
                reference_table 字段  | 
                user_id | 
              
搜索扩展属性
<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
    <attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface">
        <join reference_table="cataloginventory_stock_item" reference_field="product_id" join_on_field="entity_id">
            <field>qty</field>
        </join>
    </attribute>
</extension_attributes>
          扩展属性认证
<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>
          {
  "sku": "tshirt1",
  "price": "20.00",
  "description": "New JSmith design",
  "extension_attributes": {
    "logo size": "small"
  },
  "custom_attributes": {
    "artist": "James Smith"
  }
}
          {
  "sku": "tshirt1",
  "price": "20.00",
  "description": "New JSmith design",
  "extension_attributes": {
    "logo size": "small",
    "stock_item" : {
      "status" : "in_stock"
      "quAntity": 70
    }
  },
  "custom_attributes": {
    "artist": "James Smith"
  }
}
          扩展接口
interface CustomerExtensionInterface extends \Magento\Framework\Api\ExtensionAttributesInterface
            {
            }
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
    <attribute code="attributeName" type="Magento\Some\Type[]" />
</extension_attributes>
          相关话题
- Web API认证概述
 - 添加扩展属性到实体