Here’s a little something I figured out just recently. Let’s say you have Ubercart installed. Let’s say also that you want to add a custom field to the uc_orders table. There are a couple of ways to do this.
One is to hack the uc_orders.install file’ and add the field to its schema definition. That seems untidy, and of course not upgrade-safe. Not to mention it won’t work if Ubercart is already installed. Wouldn’t you need to uninstall/disable uc_orders, drop the orders table, then reinstall it with the new scheme?
THere’s a better way, though. What I’ve done is write a separate module. Call it “mymodule,” just for originality. In mymodule.install, use db_add_field() like so:
mymodule_enable() { $ret = array(); db_add_field($ret, 'uc_orders', 'newfield', array( 'type'=>'varchar', 'length'=>50) ); }
Simple! So now I’ve added a field to the database. Calling uc_order_load() works fine: the new fields are part of the order object since it just reads from the orders table; however, calling uc_order_save() will not update the new field.
There’s one more thing I needed to do. uc_order_save() calls drupal_write_record(), which depends on the schema. So mymodule needs to add its new field to the schema, using hook_schema_alter():
So what you do is write the following in mymodule.module:
mymodule_schema_alter(&$schema) { $schema['uc_orders']['fields']['newfield'] = array( 'type' => 'varchar', 'length' => 50, 'description' => 'custom field', ); }
And it works! I’m wondering if I could put this code in the .install file instead, in mymodule_schema(). Would that work? None of the documentation I’ve read says anything pro or con.