magento2的eav模型和扩展属性介绍

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

magento2提供了一些扩展属性的方法

  • 自定义和EAV (Entity-Attribute-Value) 属性.自定义属性是那些商家添加的属性。例如,商家可能需要添加属性来描述产品自定义属性EAV属性的子集。

Magento提供了两种类型的属性集成商可以使用扩展中提供的功能属性:

什么是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
Magento\CatalogInventory\Api\Data\StockItemInterface
ref可选。使用指定权限限制对扩展属性的访问。Magento_CatalogInventory::cataloginventory
reference_table参与联接操作的表。查看 搜索扩展属性 .admin_user
reference_fieldreference_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>
如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。