3

I imported 8500+ external users from an external user table using Feeds module successfully. Now, I am trying to add some more attributes to these users (ie. age, gender, ...). I proceeded to create several new fields for my users under admin/configuration/account settings/manage fields (ie. Age, Gender, ...). However, these fields are now all empty and it would not be possible for me to manually edit these fields for all users.

Could someone please tell me where D7 stores these new fields in the database (which table)? I would like to use a SQL script to automatically update the users with these fields.

PS. Is using a SQL script to update these information the correct approach?

5

Assuming you're using the SQL storage backend they're stored in the field_data_field_FIELDNAME and field_revision_field_FIELDNAME tables. The field system in Drupal 7 is generic across all entities so field data for nodes/users/all entity types is stored in the same tables.

Rather than input directly into those tables, though, I'd suggest using the Drupal API to load, alter and then save each user instead. The code might look something like this:

// Grab all the uids in the users table
$uids = db_query('SELECT uid FROM {users}')->fetchCol();

// Load the users
$users = user_load_multiple($uids);

// Loop through and edit each user
foreach ($users as $user) {
  // Load the extra data from whatever source you're using
  $extra_data = my_function_to_get_extra_data_by_user_id($user->uid);

  // Update the fields
  $user->field_FIELDNAME1[$user->language][0]['value'] = $extr_data['FIELDNAME1'];
  $user->field_FIELDNAME2[$user->language][0]['value'] = $extr_data['FIELDNAME2'];
  // etc...

  // Save the user object
  user_save($user);
}

You'll probably want to change this code to process the users in batches as it's likely to take a long time to run.

  • Clive, thank you for that. Do you have a good tutorial on how to use the Drupal API to load something like this? – chlong Mar 22 '12 at 18:43
  • 2
    @chlong You beat me to it, I was adding that to the answer :) The best place to look for any API examples is the Examples module, it's got loads of modules for doing all sorts of things – Clive Mar 22 '12 at 18:46
  • oh that's so cool...i had no idea that existed thank you thank you. i guess i would have to create my own module for it? – chlong Mar 22 '12 at 18:52
  • 2
    Yes you would, but it's really not that hard...have a look at the Module developer's guide to get yourself started :) – Clive Mar 22 '12 at 18:56
  • 1
    Sorry it doesn't look like there is one in there, I could have sworn there was! Have a look at this article, it's talking about nodes specifically but the principle is exactly the same when it comes to adding/updating fields for users. – Clive Mar 22 '12 at 20:16
0

The Migrate module is very helpful with these kind of tasks. User fields have to be created first.

The migrate module provides a flexible framework for migrating content into Drupal from other sources (e.g., when converting a web site from another CMS to Drupal). Out-of-the-box, support for creating core Drupal objects such as nodes, users, files, terms, and comments are included - it can easily be extended for migrating other kinds of content. Content is imported and rolled back using a bundled web interface (Migrate UI module) or included Drush commands (strongly recommended).

  • @remy..I have had a very bad experience with migrating users, but ended up accomplishing the task using Feeds module in 10 minutes. Thanks though! – chlong Mar 22 '12 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.