I am more thankful for your project each day.

I have a fairly simple set of tables for oral histories, essentially it is made up of multiple collections, each consisting of many audio (or video) tapes, each of which can consist of many segments or clips. The program used for clipping and indexing the audio files maintains collection-id, audio-id, and clip-id so these can get carried over in the migrate. I was going to write code that would link the tables based on those id values, and maybe some code that would populate a node reference field. But after seeing the issue:

http://drupal.org/node/426548

I was wondering if a node reference field in each of the drupal content-types that I'm creating as the target of the migration could be auto-populated as part of the migration process?

Comments

vrteach’s picture

Well, I didn't work out how to do this within migrate, or drupal. But I was able to write my own code that populates the node reference fields after the migrate, based on the *_node_map tables created by tw/migrate. I'm using the EZsql library for my mysql interface. The code is icky because I built it on the fly late last night.

/*
 * first build an array of the Oral History session ID and Nodes
*/
$sql = "select * from oh_session_node_map";
$sessions = $db->get_results($sql);

foreach ($sessions as $session) {
  $sessionID = $session->SessionID;
  $sessionArray[$sessionID] = $session->nodeid;
}
/*
 * Next get a list of nodes that have the cck field sessionid (three tables share the 
 * same field, two of which I'm worried about)
 */

$sql = "select * from content_field_sessionid";
$nodeswithsessions = $db->get_results($sql);

/*
 * Run through the nodes, get the value of sessionid, and use the
 * array built above to move the node reference to the content_field_sessionid (two
 * tables share this field)
 */

foreach ($nodeswithsessions as $node) {
  $nid = $node->nid;
  $sessionID = $node->field_sessionid_value;
  $sql = "select * from content_field_sessionnid where nid = $nid";
  $thisone = $db->get_row($sql);
  $gotNID = $thisone->field_sessionnid_nid;
  if ($nid) {
  $sql = "update content_field_sessionnid set field_sessionnid_nid = ".$sessionArray[$sessionID]." where nid = ".$thisone->nid ;
  $db->query($sql);
  }
}

/*
 * Now do pretty much the same thing with audioID
 */

$sql = "select * from oh_audio_node_map";
$audios = $db->get_results($sql);

foreach ($audios as $audio) {
  $audioID = $audio->AudioID;
  $audioArray[$audioID] = $audio->nodeid;
}

$sql = "select * from content_field_audioid";
$nodeswithaudios = $db->get_results($sql);
foreach ($nodeswithaudios as $node) {
  $nid = $node->nid;
  $audioID = $node->field_audioid_value;
  $sql = "select * from content_type_oh_clipper where nid = $nid";
  $thisone = $db->get_row($sql);
  $gotNID = $thisone->field_audioid_nid;
  if ($nid) {
  $sql = "update content_type_oh_clipper set field_audionid_nid = ".$audioArray[$audioID]." where nid = ".$thisone->nid ;
  $db->query($sql);
  }
}
moshe weitzman’s picture

Status: Active » Fixed

In your prepare hook, you do whatever querying you need to do to find the referenced nid. Then you set a value for the node reference field such as $node->field_training_id[0]['value'] = $training_id;. You have to run your content sets in the right order so that the referenced nid gets created before the referer.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.