You are here

function commerce_invoice_update_7101 in Commerce Invoice 7

Add the uid field to the schema

File

./commerce_invoice.install, line 63

Code

function commerce_invoice_update_7101(&$sandbox) {
  if (!db_field_exists('commerce_invoice', 'uid')) {
    $uid = array(
      'description' => 'The {users}.uid that owns this invoice.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    );
    db_add_field('commerce_invoice', 'uid', $uid);
    $result = db_select('commerce_invoice', 'c')
      ->fields('c')
      ->execute();
    while ($record = $result
      ->fetchAssoc()) {
      $invoice_id = $record['invoice_id'];
      $order_id = $record['order_id'];
      $order_result = db_select('commerce_order', 'c')
        ->fields('c')
        ->condition('order_id', $order_id)
        ->execute();
      $order_fields = $order_result
        ->fetchAssoc();
      db_update('commerce_invoice')
        ->fields(array(
        'uid' => $order_fields['uid'],
      ))
        ->condition('invoice_id', $invoice_id)
        ->execute();
    }
  }
}