请指定如何在单个 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;
}
如果您在这方面需要任何帮助,请告诉我。
蝴蝶不菲