1

I have created new column in customer_entity table

class UpgradeData implements UpgradeDataInterface{

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(),'1.0.6'<0)){

            $customerName = $setup->getTable('customer_entity');

            if ($setup->getConnection()->isTableExists($customerName) == true) {

                $connection = $setup->getConnection();

                $connection->addColumn(
                    $customerName,
                    'family_name',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'length' => 100,
                        'nullable' => true,
                        'comment' => 'Family Name'
                    ]
                );
            }
        }
}

and i am creating a customer programmatically.

$customerFactory = $this->_objectManager->get('\Magento\Customer\Model\CustomerFactory');
$newCustomer = $customerFactory->create(); 
$newCustomer->setWebsiteId($this->_storeManager->getWebsite()->getId());
$newCustomer->setEmail('email@email.com');
$newCustomer->setFirstname('First name');
$newCustomer->setLastname('Last name');
$newCustomer->setPassword('Password');
.....
$newCustomer->setData('family_name', $externalUser['FamilyName']);
$newCustomer->save();

But only family_name (custom field) is not storing in customer_entity table.

Is this the proper way to store custom field ?

$newCustomer->setData('family_name', $externalUser['FamilyName']);

any idea?

1

check below code

If already created InstallSchema.php in your module then create UpgradeSchema.php for creating a new column in customer_entity table

File: Vendor/Module/Setup/UpgradeSchema.php

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

/**
 * Class UpgradeSchema
 */
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.6') < 0) {

            $setup->getConnection()->addColumn(
            $setup->getTable('customer_entity'),
            'family_name',
            [
                'type' => Table::TYPE_TEXT,
                'nullable' => true,
                'comment' => 'Family Name'
            ]
            );
        }
    }
}

Then create customer attributes using InstallData or UpgradeData. If already exist InstallData then create UpgradeData for creating customer attribute

File:-Vendor/Module/Setup/UpgradeData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Customer\Api\CustomerMetadataInterface;

/**
 * @codeCoverageIgnore
 */
class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var CustomerSetupFactory 
     */
    private $customerSetupFactory;

    /**
     * @var SalesSetupFactory
     */
    private $salesSetupFactory;

    /**
     * @var QuoteSetupFactory
     */
    private $quoteSetupFactory;

    /**
     * @var AttributeSetFactory 
     */
    private $attributeSetFactory;

    /**
     * @var \Magento\Eav\Setup\EavSetupFactory 
     */
    private $eavSetupFactory;


    /**
     * UpgradeData constructor.
     * @param CustomerSetupFactory $customerSetupFactory
     * @param SalesSetupFactory $salesSetupFactory
     * @param QuoteSetupFactory $quoteSetupFactory
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     * @param \Magento\Eav\Model\Config $eavConfig
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        SalesSetupFactory $salesSetupFactory,
        QuoteSetupFactory $quoteSetupFactory,
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {


        $installer = $setup;
        $installer->startSetup();

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);


        if (version_compare($context->getVersion(), '1.0.6') < 0) {

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();


            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'family_name', [
                'type' => 'static',
                'label' => 'Family Name',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);

            $eavSetup->addAttributeToSet(
                CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
                CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
                null,
                'family_name');

            $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'family_name')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer','customer_address_edit','adminhtml_customer_address', 'customer_register_address','customer_account_edit', 'customer_account_create']
                ])->save();;

        }

        $installer->endSetup();
    }
}

I was Save attribute in observer using below code but it helps you for saving custom attribute

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Psr\Log\LoggerInterface;

class CustomerRegister implements ObserverInterface
{
    protected $customerFactory;
    protected $customerRepository;
    protected $logger;

    public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        CustomerRepositoryInterface $customerRepository,
        LoggerInterface $logger
    ){
        $this->customerFactory = $customerFactory;
        $this->customerRepository = $customerRepository;
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $customer = $observer->getCustomer();
        if(!$customer instanceof \Magento\Customer\Model\Customer){
            $customer = $this->customerFactory->create()->load($customer->getId());
        }

        /* Save customer Custom*/
        $controller = $observer->getAccountController();
        $family_name = $controller->getRequest()->getParam('family_name');

        $customer->setData('family_name', $family_name)
        ->save();

        $customer = $this->customerRepository->getById($customer->getId());
        $customer->setCustomAttribute('family_name', $family_name);
        $this->customerRepository->save($customer);

    }
}

I hope this code helps you to save customer custom attributes.

0

Replace this line of code

 $newCustomer->setData('family_name', $externalUser['FamilyName']);

with the following line of code:

$newCustomer->setFamilyName( $externalUser['FamilyName']);

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.