> Magento2中文手册 > 向实体添加扩展属性

向实体添加扩展属性

第三方开发者不能在Magento 2的核心改变API数据接口,这样一种方式来影响接口使用配置添加扩展属性。

我们将在产品实体、产品存储库和web API示例上演示这一点。

产品响应:

<product>
    <id>1</id>
    <sku>some-sku</sku>
    <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
    <extension_attributes><!-- Here should we add 扩展属性 data --></extension_attributes>
</product>

产品列表响应:

<products>
    <item>
        <id>1</id>
        <sku>some-sku</sku>
        <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
        <extension_attributes><!-- Here should we add 扩展属性 data --></extension_attributes>
    </item>
    <item>
        <id>2</id>
        <sku>some-sku-2</sku>
        <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
        <extension_attributes><!-- Here should we add 扩展属性 data --></extension_attributes>
    </item>
</products>

添加拦截器到产品仓库

 <?PHP
       public function afterGet
        (
            \Magento\Catalog\Api\ProductRepositoryInterface $subject,
            \Magento\Catalog\Api\Data\ProductInterface $entity
        ) {
            $ourCustomData = $this->customDataRepository->get($entity->getId());

            $extensionAttributes = $entity->getExtensionAttributes(); /** get current 扩展属性 from entity **/
            $extensionAttributes->setOurCustomData($ourCusomData);
            $entity->setExtensionAttributes($extensionAttributes);

            return $entity;
        }

    ?>
 <?php
           public function afterSave
            (
                \Magento\Catalog\Api\ProductRepositoryInterface $subject,
                \Magento\Catalog\Api\Data\ProductInterface $entity
            ) {
                $extensionAttributes = $entity->getExtensionAttributes(); /** get current 扩展属性 from entity **/
                $ourCustomData = $extensionAttributes->getOurCustomData();
                $this-customDataRepository->save($ourCustomData);

                return $entity;
            }

        ?>
use Magento\Catalog\Api\Data\ProductExtensionInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductExtensionFactory;

class ProductAttributesLoad
{
    /**
     * @var ProductExtensionFactory
     */
    private $extensionFactory;

    /**
     * @param ProductExtensionFactory $extensionFactory
     */
    public function __construct(ProductExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    /**
     * Loads product entity 扩展属性
     *
     * @param ProductInterface $entity
     * @param ProductExtensionInterface|null $extension
     * @return ProductExtensionInterface
     */
    public function afterGetExtensionAttributes(
        ProductInterface $entity,
        ProductExtensionInterface $extension = null
    ) {
        if ($extension === null) {
            $extension = $this->extensionFactory->create();
        }

        return $extension;
    }
}

And now need to bind our plugin to ProductInterface:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\Data\ProductInterface">
        <plugin name="ProductExtensionAttributeOperations" type="Magento\Catalog\Plugin\ProductAttributesLoad"/>
    </type>
</config>

扩展属性配置:

<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="first_custom_attribute" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
        <attribute code="second_custom_attribute" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
    </extension_attributes>
</config>

non-scalar 属性:

在第一种情况下,我们将得到下一个结果:

<product>
    <id>1</id>
    <sku>some-sku</sku>
    <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
    <extension_attributes>
        <first_custom_attribute>1</first_custom_attribute>
        <second_custom_attribute>2</second_custom_attribute>
    </extension_attributes>
</product>

第二章情况:

<product>
    <id>1</id>
    <sku>some-sku</sku>
    <custom_attributes><!-- Custom Attributes Data --></custom_attributes>
    <extension_attributes>
        <our_custom_data>
                <first_custom_attribute>1</first_custom_attribute>
                <second_custom_attribute>2</second_custom_attribute>
        </our_custom_data>
    </extension_attributes>
</product>