seavers
seavers

Reputation: 571

Unable to set custom attribute for sales_flat_order table

I am using Magento ver. 1.6.2.0.

After reading a number of posts and question (on SO and elsewhere) I have attempted to add an attribute to my sales_flat_order table. In previous versions of Magento, the sales/order model used the EAV method, but from what I can gather, after 1.4 it was moved to a flat table.

So, here's my mysql-install file:

$installer = $this;
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'khaos_soc', 'varchar(255) NULL');

Which has added a column to my sales_flat_order table.

My problem is that I cannot set data to be inserted into that field. I have an observer, that is called by the event 'checkout_submit_all_after'. The code retrieves the current order id, and then attempts to set the 'khaos_soc' data, here's the code:

$test = Mage::getModel('sales/order')->load($order->getId());
// write the khaos soc to the database
$test->setKhaosSoc('just testing');
$test->setCustomerFirstname('Charlie');
$test->save();

The setCustomerFirstname() method works, and changes the firstname to Charlie, but the setKhaosSoc() method doesn't save anything to the database. I have also tried using the setData('khaos_soc', 'just testing') way of doing things which doesn't work either.

I have tested this by manually setting a value for 'khaos_soc' in the database, then called the appropriate order and echoed out the getKhaosSoc() result, and it worked, but I am still unable to set it.

So, I can retrieve manually created entries for my custom attribute, but I can't set them. Can anyone suggest why that is?

Kind regards,

James


I have tried the approach suggest by Zyava, but it still doesn't save. Here's my upgrade code:

$installer = $this;
$installer->startSetup();
$installer->addAttribute(
    'order', 
    'khaos_soc', 
    array(
        'type' => 'varchar', /* varchar, text, decimal, datetime */
        'grid' => false /* or true if you wan't use this attribute on orders grid page */
    )
);
$installer->endSetup();

Here's my config.xml:

<?xml version="1.0"?>
<!-- 
/**
 * @category   Symphony
 * @package    Symphony_Khaosorders
 * @author     ModuleCreator
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
 -->
<config>
    <modules>
        <Symphony_Khaosorders>
            <version>0.1.8</version>
        </Symphony_Khaosorders>
    </modules>
    <frontend>
        <routers>
            <khaosorders>
                <use>standard</use>
                <args>
                    <module>Symphony_Khaosorders</module>
                    <frontName>khaosorders</frontName>
                </args>
            </khaosorders>
        </routers>
        <layout>
            <updates>
                <khaosorders>
                    <file>khaosorders.xml</file>
                </khaosorders>
            </updates>
        </layout>
    </frontend>
    <admin>
        <routers>
            <khaosorders>
                <use>admin</use>
                <args>
                    <module>Symphony_Khaosorders</module>
                    <frontName>khaosorders</frontName>
                </args>
            </khaosorders>
        </routers>
    </admin>
    <adminhtml>
        <menu>
            <khaosorders module="khaosorders">
                <title>Khaosorders</title>
                <sort_order>71</sort_order>               
                <children>
                    <items module="khaosorders">
                        <title>Manage Items</title>
                        <sort_order>0</sort_order>
                        <action>khaosorders/adminhtml_khaosorders</action>
                    </items>
                </children>
            </khaosorders>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <Symphony_Khaosorders>
                            <title>Khaosorders Module</title>
                            <sort_order>10</sort_order>
                        </Symphony_Khaosorders>
                    </children>
                </admin>
            </resources>
        </acl>
        <layout>
            <updates>
                <khaosorders>
                    <file>khaosorders.xml</file>
                </khaosorders>
            </updates>
        </layout>
    </adminhtml>   
    <global>
        <models>
            <khaosorders>
                <class>Symphony_Khaosorders_Model</class>
                <resourceModel>khaosorders_mysql4</resourceModel>
            </khaosorders>
            <khaosorders_mysql4>
                <class>Symphony_Khaosorders_Model_Mysql4</class>
                <entities>
                    <khaosorders>
                        <table>khaosorders</table>
                    </khaosorders>
                </entities>
            </khaosorders_mysql4>
        </models>
        <resources>
            <khaosorders_setup>
                <setup>
                    <module>Symphony_Khaosorders</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </khaosorders_setup>
            <khaosorders_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </khaosorders_write>
            <khaosorders_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </khaosorders_read>
        </resources>
        <blocks>
            <khaosorders>
                <class>Symphony_Khaosorders_Block</class>
            </khaosorders>
        </blocks>
        <helpers>
            <khaosorders>
                <class>Symphony_Khaosorders_Helper</class>
            </khaosorders>
        </helpers>
    </global>
</config>

And here's my observer method:

$test = Mage::getModel('sales/order')->load($order->getId());
// write the khaos soc to the database
$test->setKhaosSoc('just testing');
$test->setCustomerFirstname('Charlie');
$test->save();

Is there something I am missing?

Upvotes: 2

Views: 6184

Answers (3)

Roman Snitko
Roman Snitko

Reputation: 3655

You should use Mage_Sales_Model_Resource_Setup::addAttribute() method, because it takes care of modifying flat tables. And you shouldn't do it manually, calling addColumn

Upvotes: 3

kalenjordan
kalenjordan

Reputation: 2446

It's probably your cache. Even if you have caching disabled, or have cleared cache via the admin, I believe there is DB caching that still happens. Try rm -rf var/cache/* and see if that does the trick.

Upvotes: 3

Dmytro Zavalkin
Dmytro Zavalkin

Reputation: 5277

You should do it another way:

  1. Check in your config.xml that your install model type is Mage_Eav_Model_Entity_Setup:

<global>
     <resources>
         <your_module_setup>
              <setup>
                  <module>Your_Module</module>
                  <class>Mage_Sales_Model_Mysql4_Setup</class>
              </setup>
         </your_module_setup>
     </resources>
 </global>
  1. In your module upgrade script:

$installer = $this;
$installer->startSetup();
$installer->addAttribute(
    'quote_item', 
    'your_attribute_code', 
    array(
        'type' => 'int', /* varchar, text, decimal, datetime */,
    )
);
$installer->endSetup();

All credits to Magento - Adding a new column to sales_flat_quote_item and sales_flat_order_item`enter code here

Upvotes: 1

Related Questions