微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在InstallData Magento 2中添加多个属性

如何解决如何在InstallData Magento 2中添加多个属性

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

解决方法

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

以下内容将添加一些客户属性。 添加此bin / magento setup:upgrade命令后。

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

<?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','attribute_2'
        );

        $attribute->addData(
            [
                'attribute_set_id' => $attributeSetId,'used_in_forms' => ['adminhtml_customer']
            ]
        );

        $attribute->save();
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

如果您需要任何帮助,请告诉我。

,

感谢使用补丁程序的上述解决方案。它正在工作,我根据需要使用了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,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','required' => true,'visible' => true,'sort_order' => 991,'position' => 991,'system' => 0,]
);

$attribute = $customerSetup->getEavConfig()->getAttribute(
Customer::ENTITY,'phonenumber'
);

$attribute->addData(
[
'attribute_set_id' => $attributeSetId,'used_in_forms' => ['adminhtml_customer','customer_account_create','customer_account_edit']
]
);

$attribute->save();

/** attribute_2 */
$customerSetup->addAttribute(
Customer::ENTITY,'gstnumber','label' => 'GST Number','sort_order' => 992,'position' => 992,'gstnumber'
);

$attribute->addData(
[
'attribute_set_id' => $attributeSetId,'customer_account_edit']
]
);

$attribute->save();


}
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。