2

I am trying to add extension attributes to customer entity.

I set extension_attributes.xml as follows:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="my_attribute" type="Text">
            <join reference_table="my_entity"
                  reference_field="customer_id"
                  join_on_field="entity_id">
                <field>my_entity_attribute</field>
            </join>
        </attribute>
    </extension_attributes>
</config>

The table my_entity exists and it has a column called customer_id and I want to join this field to the entity_id field in the customer_entity table, and add my_entity_attribute value as the ultimate extension attribute to customer.

What am I doing wrong?

To test this, I am doing a GET rest api call to endpoint customers/me and debugging $this->_getExtensionAttributes() inside \Magento\Customer\Model\Data\Customer::getExtensionAttributes(), but it gives null.

1 Answer 1

2

Type Text is not valid, use string instead. Then remember to clear var/generation, var/cache and var/di, to have changes applied.

Also see this answer to get understanding what actions are required to get extension attribute loaded during get and getList. Join directive will work for getList only.

7
  • Hi Alex P I have all you suggested using string, clearing all the var dir, and compiling it using bin/magento setup:di:compile. The file var/generation/Magento/Customer/Api/Data/CustomerExtension.php is created correctly. However I cannot see the extension_attributes in the API call and I still get null when calling $this->_getExtensionAttributes() inside \Magento\Customer\Model\Data\Customer::getExtensionAttributes(). Any other suggestion?
    – awavi
    Dec 2, 2015 at 16:27
  • Plugins should be added to populate extension attributes. Please read comments to my answer in another post. Dec 2, 2015 at 16:50
  • Ok, but I do not see the purpose of the extension attributes then. Shouldn't I just go only with the plugin?
    – awavi
    Dec 3, 2015 at 20:05
  • If the goal is to make field accessible via REST, then yes, it is mandatory. One of the reasons for extension attributes introduction, was to make it possible to extend web APIs. As soon as you declare extension attributes, they will become visible in WSDL (SOAP) and REST documentation. Unknown fields will be just filtered out by web API during request/response processing. Dec 3, 2015 at 20:26
  • 2
    So if I understand it correctly: you always need to create a plugin as well? Just creating an EAV-attribute in your setup script and declaring it in your extension_attributes.xml doesn't make it visible for getExtensionAttributes()? Nov 16, 2016 at 14:29

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.