mediarox/module-catalog-attribute-setup
Composer 安装命令:
composer require mediarox/module-catalog-attribute-setup
包简介
Contains an alternative attributes setup class that includes an optimized create/update mechanism.
README 文档
README
Description
A Magento 2 module/library that contains an alternative attributes setup class. It is intended to simplify the creation/updating of product and category attributes.
Installation
composer require mediarox/module-catalog-attribute-setup bin/magento setup:upgrade
Usage
In most use cases we "use" the supplied setup class
use Catalog\AttributeSetup\Setup\AttributeSetup;
or more specific
use Catalog\AttributeSetup\Setup\AttributeSetupFactory;
inside a patch file (Recurring or DataPatch).
Our goal was that each patch that wants to install/update attributes only needs to provide an array of information. Everything else is then taken care of by the setup class.
To make the point effectively, take note of the following example.
Example 1 - RecurringData patch
Force product attribute setting with recurring data patch (useful for development/not released projects).
<your_module>/Setup/RecurringData.php
use Magento\Catalog\Model\Product; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Catalog\AttributeSetup\Setup\AttributeSetup; use Catalog\AttributeSetup\Setup\AttributeSetupFactory; class RecurringData implements InstallDataInterface { protected AttributeSetupFactory $attributeSetup; public function __construct( AttributeSetupFactory $attributeSetup ) { $this->attributeSetup = $attributeSetup; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** @var AttributeSetup $attributeSetup */ $attributeSetup = $this->attributeSetup->create([ 'setup' => $setup, 'attributeData' => $this->getAttributes() ]); $setup->startSetup(); $attributeSetup->addUpdateAttributes(); $setup->endSetup(); } public function getAttributes() : array { return [ Product::ENTITY => [ 'manufacturer' => [ 'is_filterable' => 0 ] ] ]; } }
Example explanations
- We always use the "getAttributes()" method internally to provide the attribute information. Feel free to do this differently.
- RecurringData patches use the "install" method as the main entry point. (In DataPatches, the "apply" method is used).
- RecurringData patches are executed on every "bin/magento setup:upgrade". Please use a DataPatch if you want to run only once.
- As usual in Magento, we still use a factory class with which we create new instances. (AttributeSetupFactory)
- In the "install" method we first create the new instance via the internal method "create" and pass our attribute information right away.
$attributeSetup = $this->attributeSetup->create([ 'setup' => $setup, 'attributeData' => $this->getAttributes() ]);
- Then, we run the "addUpdateAttributes" method on the instance to start the create/update process.
$attributeSetup->addUpdateAttributes();
Additional notes (yes, important)
Attribute information structure
As you can see in the example, the attributes array is grouped into the respective entity. ('catalog_product' or 'catalog_category')
Attribute property names
Magento unfortunately does not use a uniform name for the attribute properties. In some cases you have to use the short form and in others the long form. As a result, we have decided to uniformly push the long form. Following this, the module loads module eav-property-mapper as a dependency to ensure that we can/must use the long form across the board. (All short and long forms)
In summary: The long form must be used. If not, the script will abort due to validation.
mediarox/module-catalog-attribute-setup 适用场景与选型建议
mediarox/module-catalog-attribute-setup 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 357 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mediarox/module-catalog-attribute-setup 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mediarox/module-catalog-attribute-setup 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 357
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-03