You have to keep below code inside your UpgradeData.php
file,
<?php
/**
* Copyright © 2015 Clounce. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace {Vendor}\{Module}\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Model\Customer;
class UpgradeData implements UpgradeDataInterface
{
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* Installs data for a module
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$attributes = [
'test'=> [
"type" => "varchar",
"label" => "Your custom label",
"input" => "text",
'sort_order' => 100,
"required" => false,
'position' => 100,
'system' => false,
//'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
],
];
foreach ($attributes as $code => $options) {
$eavSetup->addAttribute(
Customer::ENTITY,
$code,
$options
);
}
$this->installCustomerForms($eavSetup);
}
/**
* Add customer attributes to customer forms
*/
public function installCustomerForms(EavSetup $eavSetup)
{
$customer = (int)$eavSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
/**
* @var ModuleDataSetupInterface $setup
*/
$setup = $eavSetup->getSetup();
$attributeIds = [];
$select = $setup->getConnection()->select()->from(
['ea' => $setup->getTable('eav_attribute')],
['entity_type_id', 'attribute_code', 'attribute_id']
)->where(
'ea.entity_type_id IN(?)',
[$customer]
);
foreach ($eavSetup->getSetup()->getConnection()->fetchAll($select) as $row) {
$attributeIds[$row['entity_type_id']][$row['attribute_code']] = $row['attribute_id'];
}
$data = [];
$attributes = [
'test'=> [
"type" => "varchar",
"label" => "Your custom label",
"input" => "text",
'sort_order' => 100,
"required" => false,
'position' => 100,
'system' => false,
//'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
],
];
foreach ($attributes as $attributeCode => $attribute) {
$attributeId = $attributeIds[$customer][$attributeCode];
$attribute['system'] = isset($attribute['system']) ? $attribute['system'] : true;
$attribute['visible'] = isset($attribute['visible']) ? $attribute['visible'] : true;
if ($attribute['system'] != true || $attribute['visible'] != false) {
$usedInForms = ['customer_account_create','adminhtml_customer','checkout_register','customer_account_edit','adminhtml_checkout'];
foreach ($usedInForms as $formCode) {
$data[] = ['form_code' => $formCode, 'attribute_id' => $attributeId];
}
}
}
if ($data) {
$setup->getConnection()->insertOnDuplicate($setup->getTable('customer_form_attribute'), $data);
}
}
}
Remove var folder from root and run command,
php bin/magento setup:upgrade
and
php bin/magento indexer:reindex
Check again. Its working.
Try that code to save custom attribute of customer
$customerRepository = $this->objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById('customer_id');
$customer->setCustomAttribute("is_checked",'0');
$customerRepository->save($customer);
Best Answer
You will have to create custom attribute in a standard way for customer entity. If your custom attribute's data type is static then it will be visible in 'customer_entity" table otherwise it will visible in respective customer entity data type table (e.g. customer_entity_int for int data type).
The 'customer_grid_flat' table is an additional table in which you can add your custom attribute field to display it on admin customer grid with search and filter options.
I hope this help you to understand the core concept of custom attribute of customer entity. Once you follow this your custom attribute's value will start getting saved in the respective database table as explained.