You are here

function nodereference_update_2 in Content Construction Kit (CCK) 5

Populate nid column in existing node reference fields.

File

./nodereference.install, line 32

Code

function nodereference_update_2() {

  // Multi-part update
  if (!isset($_SESSION['nodereference_update_2'])) {
    $_SESSION['nodereference_update_2'] = 0;
    $_SESSION['nodereference_update_2_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_nodereference_data}"));
  }
  $limit = 20;
  $result = db_query_range("SELECT nr.nid, nfnd.vid, nfnd.field_name, nfnd.delta FROM {node_field_nodereference_data} nfnd LEFT JOIN {node_revisions} nr ON nr.vid = nfnd.vid", $_SESSION['nodereference_update_2'], $limit);
  if (db_num_rows($result) == 0) {
    unset($_SESSION['nodereference_update_2']);
    unset($_SESSION['nodereference_update_2_max']);
    return array();
  }
  while ($data = db_fetch_object($result)) {
    $_SESSION['nodereference_update_2']++;
    db_query("UPDATE {node_field_nodereference_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
  }
  return array(
    '#finished' => $_SESSION['nodereference_update_2'] / $_SESSION['nodereference_update_2_max'],
  );
}