第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 InstallData Magento 2 中添加多個(gè)屬性

如何在 InstallData Magento 2 中添加多個(gè)屬性

PHP
婷婷同學(xué)_ 2023-11-03 17:35:05
請(qǐng)指定如何在單個(gè) InstallData 腳本中添加多個(gè)屬性Magento 2 使用數(shù)據(jù)腳本添加屬性。在文件夾 Vendor/Module/Setup/Patch/Data 添加 .php 文件(例如:AddCustomerAttributes)下面將添加一些客戶屬性。添加此 bin/magento setup:upgrade 命令后是必需的。如果腳本文件正確執(zhí)行,那么將會(huì)在 patch_list 數(shù)據(jù)表中添加一個(gè)條目,當(dāng)然還有 eav 屬性表中的屬性。<?phpnamespace 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;    }如果您在這方面需要任何幫助,請(qǐng)告訴我。
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

它正在工作,我根據(jù)我的要求使用相同的方法使用 InstallData/UpgradeData.php 。


請(qǐng)檢查我的答案這會(huì)將數(shù)據(jù)庫(kù)中的數(shù)據(jù)保存在表 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();



}

}

?>



查看完整回答
反對(duì) 回復(fù) 2023-11-03
  • 1 回答
  • 0 關(guān)注
  • 161 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)