0

I treid to create customer attribute programatically and now i want to update that the attribute with different label and type . I want to know in which table it will stored . I want to remove that attribute also.

public function install(ModuleDataSetupInterface $setup,ModuleContextInterface $context)

    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'phone_number',
            [
                'type'         => 'varchar',
                'label'        => 'Mobile',
                'input'        => 'integer',
                'required'     => true,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $eavSetup->updateAttribute('customer', 'phone_number', 'type', 'varchar');
        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'phone_number');     
       $attribute->setData(
            'used_in_forms',
            ['adminhtml_customer', 'customer_account_create']

        );

        $attribute->save();
    }
3

The attribute itself stored eav_attribute table. please check with following SQL in mysql:

SELECT * FROM `eav_attribute` where attribute_code='phone_number';

And the values are store in customer_entity_varchar table.

SELECT * FROM `customer_entity_varchar` where attribute_id=ID_OF_ATTRIBUTE_FROM_EAV_TABLE 

If you want to upadte your Attribute then you need to add upgrade script. You cannot simply add in install script. Here is how upgrade script look like in app/code/VENDOR/MODULE/Setup/UpgradeData.php file:

<?php
namespace VENDOR\MODULE\Setup;


use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;

class UpgradeData implements UpgradeDataInterface
{

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        //handle all possible upgrade versions

        if (!$context->getVersion()) {
            $setup->endSetup();
        }
        if (version_compare($context->getVersion(), '2.1.0') < 0) { //VERSION OF YOUR MODULE in module.xml
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute $attribute */
            $attribute = $objectManager->create('\Magento\Eav\Model\ResourceModel\Entity\Attribute');
            $attributeId = $attribute->getIdByCode('customer','phone_number');

            $eavSetup->updateAttribute(
                1,
                $attributeId,
                'type',
                'varchar'
            );
        }
    }
}
  • It returns empty set – Bhaskar C Pothineni Aug 9 '18 at 11:31
  • 1
    Then your attributes was not created at all. If it has been created then it should be seen in eav table – Prakash Thapa Aug 9 '18 at 11:35
  • can you help me to how to do that ??? – Bhaskar C Pothineni Aug 9 '18 at 11:37
1

Kindly, Refer the below link for to create Customer Attribute in magento2

http://www.ibnab.com/en/blog/magento-2/magento-2-add-custom-eav-attribute-to-category-or-customer

0

The customer attribute you have created is stored in the table customer_entity_varchar as you had declared in your code with the "type" property. Another reference to that attribute is going to be stored at customer_entity with the entity_id as a reference.

  • select * from customer_entity_varchar ; returns empty set – Bhaskar C Pothineni Aug 9 '18 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.