I want to add one attribute to sales/order, that is the Mage_Sales_Model_Order, to store some extra data.
I found the resource of sales/order uses normal table. And when I save an order, it only saves the fields that match the columns in the table.
What is the right way to add this attribute?
I would recommend a hybrid of a few of the answers below. You definitly want to put this code in a install script via config xml but your going to want to use the Mage_Eav_Model_Entity_Setup as your setup class if you want to leverage the addAttribute function. So your config xml will look something like this...
<config>
...
<resources>
<modulename_setup>
<setup>
<module>Your_Module</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
</modulename_setup>
</resources>
...
</config>
and your install script should look something like this
$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer->startSetup();
$installer->addAttribute('sales_order', 'attributename', array(
'group' => 'General',
'label' => 'Label frontend',
'note' => '',
'type' => 'string', //backend_type
'input' => 'text', //frontend_input
'frontend_class' => '',
'source' => '',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'required' => false,
'visible_on_front' => false,
'apply_to' => 'simple',
'is_configurable' => false,
'used_in_product_listing' => false,
'sort_order' => 5,
));
$installer->endSetup();
Keep in mind this is untested code, and you might need to tweak some of the attribute options to get it to function the way you need it to.