Is there a way to set or update a node's NID?

toolman18 - October 26, 2009 - 21:10

I receive a CSV file that gets generated every month with a list of content that exists in a seperate database. Unfortunately, the CSV file lists all the content in the old database every time (and I can't change that).

So now I am stuck with a problem: is there any way to know if a certain piece of content has already been imported? It would be nice if I could use the old content IDs that are in the CSV file as the NID of the node, but I haven't been able to find a way to set or update the NID of a node.

I've thought about putting the old ID in a CCK field, but I don't know of a way to search for a node by CCK Field value in PHP.

Does anyone know how to either set/update a node's NID or search by CCK field value in php to return a node?

Thank you in advance.

After some reading and

toolman18 - November 3, 2009 - 16:04

After some reading and playing with code I figured out a way to do this:

  1. Add a CCK field to the content type (I called it "legacy_id")
  2. When parsing the upload CSV file save the old ID to this "legacy_id" field
    • $node->field_legacy_id[0]['value'] = $legacyID;
  3. Before saving the node, see if there is a node that already exists with that legacy_id value. If it does, add the nid from that node to the one we are about to save:
    <?php
        $field
    = content_fields('field_legacy_id');
       
    $db_info = content_database_info($field);
       
    //see if the node alreay exists
       
    $query = "SELECT n.nid FROM {node} n INNER JOIN {". $db_info['table'] ."} a ON a.vid = n.vid WHERE a.field_legacy_id_value = $legacyID";
       
    $result = db_query($query);
       
    $exist = db_fetch_object($result);
        if (
    $exist != NULL) $node->nid = $exist->nid;
    ?>
  4. Save the node (node_save($node);)
  5. I hope this helps someone down the road!

 
 

Drupal is a registered trademark of Dries Buytaert.