3

I'm a bit lost regarding the Magento 2 extension attributes. It's clear they offer a flexible way to extend the API/interfaces with your own attributes, but what I don't get is how to save the value to a custom database. If I look at the documentation at http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html it would seem like an automatic system is in place to automatically join your data, but it doesn't work. What am I missing to actually save and get the values?

I'm trying to add a new attribute to shipmentItems's. My extension_attributes.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="\Magento\Sales\Model\Order\Shipment\Item">
        <attribute code="imei_codes" type="string">
            <join reference_table="custom_shipment_item" reference_field="item_id" join_on_field="shipment_item_id">
                <field>imei_codes</field>
            </join>
        </attribute>
    </extension_attributes>
</config>

Next in my code I'm saving the custom attribute like this:

$extensionAttributes = $shipmentItem->getExtensionAttributes();
$extensionAttributes->setImeiCodes($imeiCodes[$orderItem->getId()]);
$shipmentItem->setExtensionAttributes($extensionAttributes);
$shipmentItem->save();

My custom database gets installed like this:

$table = $installer->getConnection()
    ->newTable($installer->getTable('custom_shipment_item'))
    ->addColumn(
        'shipment_item_id',
         Table::TYPE_INTEGER,
         null,
         ['primary' => true, 'identity' => true, 'nullable' => false],
         'Primary ID of table'
     )
     ->addColumn(
         'imei_codes',
         Table::TYPE_TEXT,
         null,
         ['nullable' => true],
         'IMEI Codes'
     )
     ->setComment('Custom Extended Shipment item Table');

$installer->getConnection()->createTable($table);

The table is properly installed, but the data is not saved automatically to this table. What do I need in order to have this working? Is this the way to go to save some attribute related to shipment items?

Thank you very mucht for you assistance!

1 Answer 1

0

Currently, extension attributes are only about Web API. You need to save extension attributes manually using plugins on appropriate services.

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.