I'm migrating roughly a thousand users from another site into a brand new drupal site. I'm going through a database and calling user_save(...); with the appropriate info.

This works great.

However, in the content profile I have things like their address, phone number, bio, all sorts of that information attached to a node. I know how to create a normal node programmatically, but how do I create a content profile node programmatically that is attached to a user?


mcaden’s picture

-= Mission Accomplished =-

I created a user, then set the uid of the node equal to the new author. That was all. For the benefit of anybody else needing this I've pasted it below (with table/column names edited to generic names).

Note: Transferring old user passwords like I am in this snippet will only work if the previous developer was FAIL and left the database passwords plain-text.


  require_once './includes/bootstrap.inc';

  $query = db_query("SELECT * FROM {OldUserTable}");

  while ($row = db_fetch_array($query) ) 
    $userData = array( 'name' => $row['ID'], 'pass' => $row['PASSWORD'], 'mail' => $row['EMAIL'], 'status' => 1, 'roles' => $role );  
      $time = time();
      $new_user = user_save('', $userData);
        $profile_node = new stdClass();
        $profile_node->title = $new_user->name;
        $profile_node->body = '';
        $profile_node->type = 'profile';   // Your specified content type
        $profile_node->created = $time;
        $profile_node->changed = $time;
        $profile_node->status = 1;
        $profile_node->promote = 0;
        $profile_node->sticky = 0;
        $profile_node->format = 1;       // Filtered HTML
        $profile_node->uid = $new_user->uid; // UID of content owner
        $profile_node->field_first_name[0]['value'] = $row['FIRST'];
        $profile_node->field_last_name[0]['value'] = $row['LAST'];
        $profile_node->field_location[0]['street'] = $row['ADDRESS'];
        $profile_node->field_location[0]['city'] = $row['CITY'];
        $profile_node->field_location[0]['province'] = $row['STATE'];
        $profile_node->field_location[0]['postal_code'] = $row['ZIP'];
        $profile_node->field_location[0]['country_name'] = 'United States';
        $profile_node->field_location[0]['phone'] = $row['PHONE'];
        $profile_node->field_location[0]['fax'] = $row['FAX'];
      catch (Exception $e) 
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    catch (Exception $e) 
      echo 'Caught exception: ',  $e->getMessage(), "\n";
mcaden’s picture

Assigned: Unassigned » mcaden
Status: Active » Fixed

Oops, forgot to change tags.

Status: Fixed » Closed (fixed)

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

NeoID’s picture

Status: Closed (fixed) » Active

Thanks for the code, but it's just generating entries in the database without ever finishing.

Has there been any changes to the module that might affect this?

fuzzy76’s picture

It's better to omit the format when saving nodes. That way, node_save() will use the default format for the content type. I used your code, but ran into weird permission problems. After a lot of debugging, I noticed our site does not have an input filter with ID 1 at all.

brandonratz’s picture

My use case:
List of contacts with extensive 'content profile' information (no user).
When a new user registers.
If their profile exists; tie the user to the existing content profile...

Any ideas?

froto’s picture

This is brilliant!

Almost exactly what I need...except...I am working with /node/add/profile as admin user and I want to create an entry in users table for a newly created content profile instead of the Content Profile module trying to link it up the admin user. Basically, I want to give admin user ability to create a new user + their content profile... this is proving to be a nightmare ...

How can I accomplish this outside of modifying Content Profile modules hook_node_api ...??

Desperate for help....