There must be something that I'm not seeing, some custom attribute I added to the customer_address_entity table isn't not saved.
The idea is to from admin form add then (get/save) custom attribute into customer_address_entity
app/code/Mag/Ento/etc/db_schema.xml
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd"> <table name="customer_address_entity" resource="default"> <column xsi:type="int" name="mr_external_id" unsigned="true" nullable="true" comment="External id"/> </table> </schema> app/code/Mag/Ento/etc/extension_attributes.xml
<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\AddressInterface"> <attribute code="mr_external_id" type="int" /> </extension_attributes> </config> app/code/Mag/Ento/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Mag\Ento\Api\Data\CustomerAddressExtensionInterface" type="Mag\Ento\Model\CustomerAddressExtension"/> <type name="Magento\Customer\Api\AddressRepositoryInterface"> <plugin name="mag_ento_customer_address_extension_custom_attr_save" type="Mag\Ento\Plugin\CustomerAddressRepositoryPlugin" sortOrder="10"/> </type> </config> app/code/Mag/Ento/Api/Data/CustomerAddressExtensionInterface.php
namespace Mag\Ento\Api\Data; use Magento\Framework\Api\ExtensionAttributesInterface; interface CustomerAddressExtensionInterface extends ExtensionAttributesInterface { public const MR_EXTERNAL_ID = 'mr_external_id'; public const MR_ADDRESS_ID = 'mr_address_id'; /** * @return int|null */ public function getMrExternalId(): ?int; /** * @param int|null $value * @return CustomerAddressExtensionInterface */ public function setMrExternalId(?int $value): CustomerAddressExtensionInterface; } app/code/Mag/Ento/Model/CustomerAddressExtension.php
namespace Mag\Ento\Model; use Mag/Ento\Api\Data\CustomerAddressExtensionInterface; use Magento\Framework\Model\AbstractExtensibleModel; class CustomerAddressExtension extends AbstractExtensibleModel implements CustomerAddressExtensionInterface { /** * Get mrExternalId * * @return int|null */ public function getMrExternalId(): ?int { $id = $this->getData(self::MR_EXTERNAL_ID); return $id !== null ? (int) $id : null; } /** * Set mrExternalId * * @param int|null $value * @return CustomerAddressExtensionInterface */ public function setMrExternalId(?int $value): CustomerAddressExtensionInterface { $this->setData(self::MR_EXTERNAL_ID, $value); return $this; } } Now get/save the extension attribute
app/code/Mag/Ento/Plugin/CustomerAddressRepositoryPlugin.php
namespace Mag\Ento\Plugin\CompanyAddress; use Magento\Customer\Api\AddressRepositoryInterface; use Magento\Customer\Api\Data\AddressInterface; use Magento\Customer\Api\Data\AddressInterface as CustomerAddressInterface; use Magento\Framework\Api\ExtensionAttributesFactory; class CustomerAddressRepositoryPlugin { /** * @param ExtensionAttributesFactory $extensionAttributesFactory * @param AddressRepositoryInterface $addressRepositoryInterface */ public function __construct( private readonly ExtensionAttributesFactory $extensionAttributesFactory, ) { } public function afterGetById(AddressRepositoryInterface $subject, AddressInterface $address): AddressInterface { $extensionAttributes = $address->getExtensionAttributes(); if ($extensionAttributes === null) { $extensionAttributes = $this->extensionAttributesFactory->create(CustomerAddressInterface::class); } $extensionAttributes->setMrExternalId(1234); // 1234 hard code value for test $address->setExtensionAttributes($extensionAttributes); return $address; } /** * @param AddressRepositoryInterface $subject * @param CustomerAddressInterface $address * @return array */ public function beforeSave(AddressRepositoryInterface $subject, AddressInterface $address): array { $extensionAttributes = $address->getExtensionAttributes(); $extensionAttributes->setMrExternalId(1234); // 1234 hard code value for test $address->setExtensionAttributes($extensionAttributes); return [$address]; } } Not also working with afterSave
I trigger some save action in another file in order to insert rows in customer_address_entity
$address->setFirstname('john') ... //also tried $extensionAttributes->setMrExternalId('1234'); $address->setExtensionAttributes($extensionAttributes); $this->customerAddressRepositoryInterface->save($address); All the attributes are well saved in DB except mr_external_id attribute value.
nb: What I also don't understand is that in an observer save after, the value is available in the object but not saved in db.
[data_object (Magento\Customer\Model\Address\Interceptor)] => Array ( [parent_id] => 3193 [customer_id] => 3193 [street] => addr1 [extension_attributes] => Array ( [mr_external_id] => 1234 //<------ ) ... ) I already implemented extension attributes but I don't see what's wrong in this case.
public function setMrExternalId, but when saving, you're calling$extensionAttributes->setMiExternalIdinstead.