3

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?

Thanks.

Marco

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:

<config>
    <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">
              <field>user_name</field>
           </join>            
        </attribute>
    </extension_attributes>
</config>

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

<?php
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->_logger->addDebug('AfterCustomerSaveObserver_Constructor_Begin');

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


        $this->_logger->addDebug('AfterCustomerSaveObserver_Constructor_End');
    }

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

        $this->_logger->addDebug('AfterCustomerSaveObserver_Execute_Begin');

/*      try { */

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

            $customerExtension = $customer->getExtensionAttributes(); 
            if ($customerExtension === null) { 
                $customerExtension = $this->_customerExtensionFactory->create(); 
            }       
            $customerExtension->setUserName("Test123"); 
            $customer->setExtensionAttributes($customerExtension); 

            $this->_customerRepository->save($customer);
            //$customer->save();

/*      } catch (\Exception $e) {
            $this->_logger->critical($e);
        } */

        $this->_logger->addDebug('AfterCustomerSaveObserver_Execute_Begin');
    }

}
3

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 '15 at 8:22
  • Attribute value should be saved in plugin declared for customer repository save method, no need to create observer. – Alex Paliarush Dec 30 '15 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 '15 at 10:19
  • That's a separate question related to forms, I would recommend to ask it separately. – Alex Paliarush Dec 30 '15 at 10:36
  • Ok but how do I save the extension attribute? Is it right in the observer on top? – ClassMP Dec 30 '15 at 10:43

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.