【问题标题】:How to add multiple attributes in InstallData Magento 2如何在 InstallData Magento 2 中添加多个属性
【发布时间】:2020-08-26 06:07:02
【问题描述】:

请指定如何在单个 InstallData 脚本中添加多个属性

【问题讨论】:

    标签: php magento magento2 magento-1.9


    【解决方案1】:

    Magento 2 使用数据脚本来添加属性。 在文件夹 Vendor/Module/Setup/Patch/Data 添加一个 .php 文件(例如:AddCustomerAttributes)

    下面会添加几个客户属性。 添加此 bin/magento setup:upgrade 命令后是必需的。

    如果脚本文件被正确执行,当然还有eav属性表中的属性,patch_list数据表中会添加一个条目。

    <?php
    namespace Vendor\Module\Setup\Patch\Data;
    
    use Magento\Customer\Model\Customer;
    use Magento\Customer\Setup\CustomerSetupFactory;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Framework\Setup\Patch\DataPatchInterface;
    
    class AddCustomerAttributes implements DataPatchInterface
    {
        /**
         * @var ModuleDataSetupInterface
         */
        protected $moduleDataSetup;
    
        /**
         * @var CustomerSetupFactory
         */
        protected $customerSetupFactory;
    
        /**
         * @var AttributeSetFactory
         */
        protected $attributeSetFactory;
    
        /**
         * AddCustomerPhoneNumberAttribute constructor.
         * @param ModuleDataSetupInterface $moduleDataSetup
         * @param CustomerSetupFactory $customerSetupFactory
         * @param AttributeSetFactory $attributeSetFactory
         */
        public function __construct(
            ModuleDataSetupInterface $moduleDataSetup,
            CustomerSetupFactory $customerSetupFactory,
            AttributeSetFactory $attributeSetFactory
        ) {
            $this->moduleDataSetup = $moduleDataSetup;
            $this->customerSetupFactory = $customerSetupFactory;
            $this->attributeSetFactory = $attributeSetFactory;
        }
    
        /**
         * {@inheritdoc}
         */
        public function apply()
        {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    
            $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
            /** attribute_1 */
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'attribute_1',
                [
                    'type' => 'text',
                    'label' => 'Attribute One',
                    'input' => 'text',
                    'required' => false,
                    'user_defined' => true,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'default' => 0,
                    'system' => 0
                ]
            );
    
            $attribute = $customerSetup->getEavConfig()->getAttribute(
                Customer::ENTITY,
                'attribute_1'
            );
    
            $attribute->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer']
                ]
            );
    
            $attribute->save();
    
            /** attribute_2 */
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'attribute_2',
                [
                    'type' => 'int',
                    'label' => 'Attribute Two',
                    'input' => 'select',
                    'source' => 'Vendor\Module\Model\Options',
                    'required' => false,
                    'user_defined' => true,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'default' => 0,
                    'system' => 0
                ]
            );
    
            $attribute = $customerSetup->getEavConfig()->getAttribute(
                Customer::ENTITY,
                'attribute_2'
            );
    
            $attribute->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer']
                ]
            );
    
            $attribute->save();
        }
    
        /**
         * {@inheritdoc}
         */
        public static function getDependencies()
        {
            return [];
        }
    
        /**
         * {@inheritdoc}
         */
        public function getAliases()
        {
            return [];
        }
    }
    

    如果您在这方面需要任何帮助,请告诉我。

    【讨论】:

    • 感谢您的回复。让我检查并确认它是否工作正常:)
    • 嘿,你能告诉我使用数据补丁添加自定义属性是否是一种方便的方法?能否在 installdata.php 文件中提供解决方案?
    【解决方案2】:

    感谢上述使用补丁的解决方案。它正在工作,我根据我的要求使用了相同的方法,使用 InstallData/UpgradeData.php。

    请检查我的回答 这会将数据库中的数据保存在表 customer_entity_varchar 中,并将属性保存在 eav_attribute 中。 检查代码:

    <?php
    namespace CustomB2BRFQ\Module\Setup;
    
    use Magento\Customer\Model\Customer;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Customer\Setup\CustomerSetupFactory;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
    
    class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
    {
    private $eavSetupFactory;
    
    private $eavConfig;
    
    private $attributeResource;
    
    private $customerSetupFactory;
    
    /**
    * @var AttributeSetFactory
    */
    protected $attributeSetFactory;
    
    protected $moduleDataSetup;
    
    
    public function __construct(
    \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
    \Magento\Eav\Model\Config $eavConfig,
    \Magento\Customer\Model\ResourceModel\Attribute $attributeResource,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory,
    ModuleDataSetupInterface $moduleDataSetup
    ) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
    $this->attributeResource = $attributeResource;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
    $this->moduleDataSetup = $moduleDataSetup;
    
    }
    
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    //$customerSetup->removeAttribute(Customer::ENTITY, "phonenumber");
    
    $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    
    /** attribute_1 */
    $customerSetup->addAttribute(
    Customer::ENTITY,
    'phonenumber',
    [
    'type' => 'varchar',
    'label' => 'Phone Number',
    'input' => 'text',
    'required' => true,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 991,
    'position' => 991,
    'system' => 0,
    
    ]
    );
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(
    Customer::ENTITY,
    'phonenumber'
    );
    
    $attribute->addData(
    [
    'attribute_set_id' => $attributeSetId,
    'attribute_group_id' => $attributeGroupId,
    'used_in_forms' => ['adminhtml_customer',
    'customer_account_create',
    'customer_account_edit']
    ]
    );
    
    $attribute->save();
    
    /** attribute_2 */
    $customerSetup->addAttribute(
    Customer::ENTITY,
    'gstnumber',
    [
    'type' => 'varchar',
    'label' => 'GST Number',
    'input' => 'text',
    'required' => true,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 992,
    'position' => 992,
    'system' => 0,
    ]
    );
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(
    Customer::ENTITY,
    'gstnumber'
    );
    
    $attribute->addData(
    [
    'attribute_set_id' => $attributeSetId,
    'attribute_group_id' => $attributeGroupId,
    'used_in_forms' => ['adminhtml_customer',
    'customer_account_create',
    'customer_account_edit']
    ]
    );
    
    $attribute->save();
    
    
    }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 2016-05-29
      • 2022-01-24
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多