Unable to set custom attribute for sales_flat_order table

Unable to set custom attribute for sales_flat_order table

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Unable to set custom attribute for sales_flat_order table

Solving Custom Attribute Issues for Magento Sales_flat_order Table

James has encountered an issue while trying to add a custom attribute (khaos_soc) to his sales_flat_order table in Magento ver. 1.6.2.0. Despite successfully adding the column using MySQL, he is unable to set or save data to this new field through his observer.

Understanding the Issue

In versions prior to 1.4, Magento utilized an EAV (Entity-Attribute-Value) model for the sales/order entity, but this was later moved to a flat table structure in subsequent releases. This change impacts how custom attributes are handled and saved.

James has attempted two approaches:

  1. Directly adding a column using MySQL with $installer->getConnection()->addColumn($installer->getTable('sales/order'), 'khaos_soc', 'varchar(255) NULL');

    • This successfully adds the column to the sales_flat_order table.
  2. Using an observer and the setKhaosSoc() method to save data.

    • The observer is triggered by the checkout_submit_all_after event.
    • The code attempts to load the order and set the khaos_soc attribute with a test value 'just testing'.

However, despite both approaches, James finds that the setKhaosSoc() method doesn’t save any data to the database.

Analyzing the Problem

The issue lies in the way Magento handles flat tables. When a column is added directly through MySQL, it does not automatically create an EAV-based attribute entry in the eav_attribute table necessary for saving data via the Magento API.

James has also attempted using the setData('khaos_soc', 'just testing') method and tried creating entries manually in the database without success.

The Solution: Using EAV Setup

To resolve this issue, we need to create an attribute using the EAV setup provided by Magento. Here’s how:

Step 1: Update the config.xml

In your module’s etc/config.xml file, add the following code inside the <global> tags:

<models>
    <khaosorders>
        <class>Symphony_Khaosorders_Model</class>
        <resourceModel>khaosorders_mysql4</resourceModel>
    </khaosorders>
    <khaosorders_mysql4>
        <class>Symphony_Khaosorders_Model_Mysql4</class>
        <entities>
            <khaosorders>
                <table>sales_flat_order</table>
            </khaosorders>
        </entities>
    </khaosorders_mysql4>
</models>

<resources>
    <!-- Add the following resource for setup -->
    <khaosorders_setup>
        <setup>
            <module>Symphony_Khaosorders</module>
            <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </khaosorders_setup>

    <!-- Existing resources for writing and reading -->
    <khaosorders_write>
        <connection>
            <use>core_write</use>
        </connection>
    </khaosorders_write>
    <khaosorders_read>
        <connection>
            <use>core_read</use>
        </connection>
    </khaosorders_read>
</resources>

<!-- Other blocks, helpers, and menu entries remain the same -->

Step 2: Update the install-1.6.2.0.php (or relevant version)

In your module’s sql/install-1.6.2.0.php file (assuming Magento ver. 1.6.2.0), add the following code:

$installer = $this;
$installer->startSetup();

$installer->addAttribute(
    'order',  // Entity type
    'khaos_soc',  // Attribute name
    array(     // Attribute options
        'type' => 'varchar',
        'grid' => false,
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'apply_to' => '',
    )
);

$installer->endSetup();

Ensure to update the file name if you are using a different Magento version.

Step 3: Update the Observer

In your observer method, replace the current setKhaosSoc() line with:

// Load the order object
$order = Mage::getModel('sales/order')->load($orderId);

// Set the attribute data
$order->setData('khaos_soc', 'just testing');

// Save the order
$order->save();

Conclusion

To successfully create and save custom attributes for the sales_flat_order table, you need to use Magento’s EAV setup provided through its installer script. By following the steps outlined above, you can ensure that your attribute is properly created with a corresponding entry in the eav_attribute and catalog_eav_attribute tables.

This approach allows you to easily manage and save data for your custom attributes using Magento’s built-in API and without having to manually update the flat table through MySQL.

Remember to clear your Magento cache after making these changes to ensure the updates take effect. If you encounter any further issues or need assistance with other Magento development tasks, please refer to our Magento Development Services page for more information and expert guidance from our team of experienced developers.

Happy coding!

Checkout Database Extensions Orders

Our network