Magento2 EAV and extension attributes

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

  • attribute_set_id
  • created_at
  • group_price
  • media_gallery
  • name
  • price
  • sku
  • status
  • store_id
  • tier_price
  • type_id
  • updated_at
  • visibility
  • weight

在这种状况下,当调用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
相关文章
相关标签/搜索