Veuillez spécifier comment ajouter plusieurs attributs dans un seul script InstallData

0
Rashi Goyal 26 août 2020 à 09:07

2 réponses

Meilleure réponse

Magento 2 utilise des scripts de données pour ajouter des attributs. Dans le dossier Vendor / Module / Setup / Patch / Data, ajoutez un fichier .php (par exemple: AddCustomerAttributes)

Ce qui suit ajoutera quelques attributs client. Après avoir ajouté cette configuration bin / magento: la commande de mise à niveau est requise.

Il y aura une entrée ajoutée à patch_list datatable, si le fichier de script a été exécuté correctement et aussi les attributs dans la table attributaire eav bien sûr.

<?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 [];
    }
}

Veuillez me faire savoir si vous avez besoin d'aide pour quoi que ce soit à ce sujet.

0
Paboda 26 août 2020 à 15:51

Merci pour la solution ci-dessus en utilisant des correctifs. Cela fonctionne et j'ai utilisé la même méthodologie en utilisant InstallData / UpgradeData.php selon mes besoins.

Veuillez vérifier ma réponse. Cela enregistrera les données dans la base de données dans la table customer_entity_varchar et les attributs dans eav_attribute. Vérifiez le code:

<?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();


}
}
?>
0
Rashi Goyal 28 août 2020 à 08:16