I'm looking for the best practice for extending the customer entity. I want to add some custom fields (user_name, company_infos, etc.) and I don't know if it's better to extend the customer entity with the new attributes or creating a new table to store the additional data. The goal is to visualize the custom data on the product detail page. So what do you think, which is better the solution to get the best performance?



Update: I followed the hints below, but the extension Attribute won't be saved. Do you have any suggestions to solve it?

I created an extension_attributes.xml file:

    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="user_name" type="string">
           <join reference_table="customer_extension" reference_field="customer_id" join_on_field="entity_id">

And an Observer which is registered on the "customer_register_success" event:

namespace My\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterCustomerSaveObserver implements ObserverInterface {

    protected $_logger;
    protected $_customerExtensionFactory; 
    protected $_customerRepository;

    public function __construct(
            \Psr\Log\LoggerInterface $logger, //log injection
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
            \Magento\Customer\Api\Data\CustomerExtensionFactory $customerExtensionFactory
    ) {

        $this->_logger = $logger;


        $this->_customerRepository = $customerRepository;
        $this->_customerExtensionFactory = $customerExtensionFactory;


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


/*      try { */

            $event = $observer->getEvent();
            $customer = $event->getCustomer();  

            $customerExtension = $customer->getExtensionAttributes(); 
            if ($customerExtension === null) { 
                $customerExtension = $this->_customerExtensionFactory->create(); 


/*      } catch (\Exception $e) {
        } */



1 Answer 1


Better to create separate table and use extension attributes mechanism (as described here). Then access these attributes via \Magento\Customer\Api\Data\CustomerInterface::getExtensionAttributes().

  • Thx for the Information. It helps me to get deeper into Extension Attributes. But how can I save the Extension Attributes? In my case I created an Observer which should save the Extension Attributes after saving the customer registration.
    – ClassMP
    Dec 30, 2015 at 8:22
  • Attribute value should be saved in plugin declared for customer repository save method, no need to create observer. Dec 30, 2015 at 9:34
  • I want to extend the customer registration form. How do I get the value of my "user_name" textfield, which I want to store in the extended attribute?
    – ClassMP
    Dec 30, 2015 at 10:19
  • That's a separate question related to forms, I would recommend to ask it separately. Dec 30, 2015 at 10:36
  • Ok but how do I save the extension attribute? Is it right in the observer on top?
    – ClassMP
    Dec 30, 2015 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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