Skip to content

Customer custom attributes do not save data to customer_entity table

I’m creating a rest API and I need to add a custom columns to the customer_entity table and save the value in that fields within API request.

Сreated new attributes in the customer_entity table using UpgradeSchema:

public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $installer = $setup;

    $installer->startSetup();

    if (version_compare($context->getVersion(), '1.0.0', '<=')) {
        $installer->getConnection()->addColumn(
            $installer->getTable('customer_entity'),
            'phone',
            [
                'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                'nullable' => true,
                'length' => 100,
                'comment' => 'Phone'
            ]
        );

        $installer->getConnection()->addColumn(
            $installer->getTable('customer_entity'),
            'full_name',
            [
                'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                'nullable' => true,
                'length' => 100,
                'comment' => 'Full Name'
            ]
        );
    }

    $installer->endSetup();
}

The above code created two new fields in customer_entity table. And now I have a function that accepts data from the rest api request and creates a customer, customer is created with this code, but it does not save values to customer_entity table for phone and full_name fields.

use MagentoCustomerApiCustomerRepositoryInterface;
use MagentoCustomerApiDataCustomerInterfaceFactory;

public function __construct(
    CustomerRepositoryInterface $customerRepository,
    CustomerInterfaceFactory $customerInterfaceFactory,

) {
    $this->customerRepository = $customerRepository;
    $this->customerInterfaceFactory = $customerInterfaceFactory;
}

public function createCustomer( string $fullName, string $phone, string $password )
{
    try {
        $customer = $this->customerInterfaceFactory->create();
        $customer->setEmail(uniqid( '', true ) . '@auto.gmail.com');

        $customer->setFirstname($fullName);
        $customer->setLastname($fullName);


        // Set custom attributes
        $customer->setCustomAttribute('phone', $phone);
        $customer->setCustomAttribute('full_name', $fullName);

        // Save customer
        $customer = $this->customerRepository->save($customer, $password);

        return 'Customer created successfully ' . $customer->getId();
    } catch (LocalizedException $e) {
        return 'Error creating customer: ' . $e->getMessage();
    }
}

If you know, please tell me how I can put a value in a customer custom attributes when creating a customer.