在本主题中,您将获得Magento 2中添加EAV属性的概述和清晰说明。
目录
- Magento 2 EAV属性概述
 - 设置产品Magento 2的EAV属性
- 创建
eavsetup工厂 - 添加EAV属性
 - 删除产品的eav属性
 
 - 创建
 
使用EAV系统可以自动扩展模型,而不是干预数据库。在本主题中,您将获得在Magento 2中设置EAV属性的概述和清晰说明。
Magento 2 EAV属性概述
 您的模型仍将延伸\Magento\Framework\Model\AbstractModel。 
 您的资源模型将扩展\Magento\Eav\Model\Entity\AbstractEntity实现EntityInterface和DefaultAttributesProvider接口,与标准相比的额外方法\Magento\Framework\Model\ResourceModel\Db\AbstractDb是: 
getAttribute('code')- 从实体获取属性saveAttribute($this, 'code')- 在不经过整个实体过程的情况下保存实体的属性
 在请求连接的命令中,您将使用该__construct()方法而不是__init()方法。 
public function __construct(
        \Magento\Eav\Model\Entity\Context $context,
        $data = []
    ) {
        parent::__construct($context, $data);
        $this->setType('entity_type_code');
        $this->setConnection('entity_type_code_read', 'entity_type_code_write');
    }
  此外,Magento\Eav\Model\Config如果要实现与EAV属性相关的信息,请应用该类。将考虑使用以下方法: 
getAttribute('entity_type_code', 'attribute_code')getEntityType('entity_type_code')
设置产品Magento 2的EAV属性
创建eavsetup工厂
  运行以下脚本并完成创建EavSetup工厂 
/**
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;
 
    /**
     * UpgradeData constructor
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    } 
添加EAV属性
/** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            /**
            * Add attributes to the eav/attribute
            */
            
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'is_featured',
                [
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Is Featured',
                    'input' => 'boolean',
                    'class' => '',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '1',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );
 需要遵循一些字符:
- is_featured:输入属性代码
 - group:在后端声明组的名称
 - type:设置数据类型
 - globar:定义属性的范围(商店,网站或全球)
 - visible_on_frontend:选择true或false选项以显示前端的属性
 - apply_to:指定要设置属性的确切产品类型
 
删除产品的eav属性
您还可以通过运行以下命令从产品中删除eav属性:
$entityTypeId = 4; // Find these in the eav_entity_type table
            $eavSetup->removeAttribute($entityTypeId, 'is_featured');
 主题是您需要了解Magento 2中的实体属性值(EAV)的基本和简单知识。如果您仍需要任何帮助,请在下面评论。
