I've added a static Customer Address attribute (for some reason, other didn't work, but whatever; another time):
$setup->addAttribute('customer_address', 'xxx', [
'type' => 'static',
'input' => 'text',
'label' => 'XXX',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'visible_on_front' => 1,
'group' => 'General',
// 'used_in_forms' => ['adminhtml_customer']
'used_in_forms' => ['adminhtml_customer_address']
]);
The attribute is showing up in the admin panel and, on load, the object is populated with the correct value.
But now I want to make sure this attribute gets correctly transferred to the Data model as well (\Magento\Customer\Api\Data\AddressInterface):
$x = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
/** @var \Magento\Customer\Api\Data\AddressInterface $addr */
$addr = $x->getById(1); // <-- "xxx" is not available here; others are
At first, I thought that only by having the static attribute, M2 would transfer its value to the Data model, or at least to its getCustomAttributes() or getExtensionAttribute(). It didn't. So I've created an extension_attrbutes.xml, telling it to do so:
<?xml version="1.0"?>
<config path="to.xsd">
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="xxx" type="string" />
</extension_attributes>
</config>
Damn it, that didn't work either, because I didn't specify a join directive and thus failing some conditions. But do I have to specify a join condition when my attribute is static/column? Furthermore, the Address Data interface is implemented (not auto-generated) so I think the only way is through extension attributes, right?
Do I really have to do it manually?
I'm really starting to hate those Data layers. There's not only many criterias to get an attribute functioning correctly, but, once you finally have made a valid attribute, some other find-them-if-you-can criteria need to be satisfied in order to have attributes into the Data layer.
custom_attributesproperty. Which is what I was half-expecting Address Data to be able to do, too. Coupled with the custom-vs-extension attributes, and until I get the hang of it, it's quite a complicated system. – nevvermind Nov 19 '15 at 21:44\Magento\Customer\Model\Address::getDataModel()is deprecated. I need to get to the extension attribute object, but I cannot through the Address model (as it's the case of the Product model of your github link), because of the deprecation. So it's\Magento\Customer\Api\Data\AddressInterface::getExtensionAttributes()instead of the preffered\Magento\Customer\Model\Address::getExtensionAttributes(). Er.. what now? Also, see what over-egineering gets you? – nevvermind Nov 19 '15 at 23:51afterLoadplugin, you receive a model. But I cannot get to the extension attrs object through the model, but through its data model instead - which I'm forbidden to. I commented twice to give you the full view of things. – nevvermind Nov 19 '15 at 23:54getCustomAttributes()return value pre-populated with my attribute, but it doesn't happen. When creating such an attribute (EAV, static or not) the system is not setting it. The attribute appears in backend, I set a value, I save the customer, but on refresh the value's not there. M2 checks the already implemented Address Data interface which has nosetXxxorsetIsXxx, thus failing this check: m2 link. So you end up with a valid EAV attribute, which is not saved in DB. – nevvermind Nov 21 '15 at 15:19