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');
    }

}
ClassMP
  • 1,793
  • 7
  • 25
  • 31

1 Answers1

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().

Alex Paliarush
  • 13,751
  • 5
  • 51
  • 55
  • 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 08: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 09: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
  • It is up to you how to save them, but recommended way is using plugins. See why – Alex Paliarush Dec 30 '15 at 10:45
  • In other words: Is it the right way to use the setExtensionAttributes method of the entity and save the entity with the repository class of the entitiy? – ClassMP Dec 30 '15 at 10:47
  • In controller you should set extension attribute value taken from the form. Then in plugin for repository save, you should get this value and save it to custom table. – Alex Paliarush Dec 30 '15 at 10:56
  • Ok thanks for the additional Explanation. Is there a Repository for the Extension Attributes generated too? Or how do I save the values to my custom table? – ClassMP Dec 30 '15 at 11:10
  • Logic for saving attribute data to table should be implemented, see \Magento\CatalogInventory\Model\Plugin\AroundProductRepositorySave::aroundSave as an example. – Alex Paliarush Dec 30 '15 at 11:34