2

I am using Drupal 7.

I am using the Migrate module to migrate a list of users from an external user database table into the Drupal "users" table. The problem is that the Drupal "users" table has a numerical "uid" field, which does not exist in my external database users table.

Could someone please tell me of a way to assign a unique uid to the users table everytime I map a row using Migrate?

For example, right now I have, in the "users" database:

0
1 root ...
//external users start here
2 user1 ...
3 user2 ...
4 user3 ...

Essentially I have would like to start at 2 and continue for each user I migrate over to the "users" table.

More specifically, how I would modify/add a line to the following so that the code can auto generate unique ID's starting from 2? Would the following code be a viable option? If so, where would be the appropriate place to update $counter?

$counter = 2;
$this->addFieldMapping('uid', '$counter');

Thanks.

EDIT:

If I were to use the following SQL statement to do the user migration, would it be a good idea? If it is also a viable option, how might be able to create unique "uid" for insertion into the "users" table?

INSERT INTO users (uid, name, mail,pass,...) SELECT uid, oldusername, email, md5(clear_password),... FROM old_users_table;

Thanks.

  • You might want to show your code. Migrate has a specific destination class for users which should take stuff like uid. – Berdir Mar 16 '12 at 6:33
2

Inserting data directly into Drupal tables is generally a bad idea (until you are familiar enough with the database schema to know what you're doing). For programmatically inserting users there are functions like user_save() and drupal_form_submit() that you can use.

As for generating the unique uid: if you can't just provide incremental values in the data insertion loop then you might be able to use the following code instead (copied from user_save() function):

$account->uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());

I'm not familiar with the Migrate module so I don't have more specific advice to offer currently, but the above info and links should be helpful.

  • dooshta, thanks for the comment. Could you please show me some code on how I might be able to "provide incremental values in the data insertion loop"? I currently have "MyProjectMigration extends Migration" and in there I have "$this->addFieldMapping('uid', '$uid_counter');" I don't know how to update the $uid_counter value nor do I have a loop? Am I missing something? Even if I remove the "uid" mapping, my Migration does not appear under admin/content/migration, would you happen to know why? Sorry, but this is a little frustrating first time with Migration. – chlong Mar 16 '12 at 13:43
  • Do you need to use the Migrate module for this task? If it's a simple one time user import it might be easier to write a custom script. I can't really answer your questions since I haven't worked with the Migrate module myself (it looks pretty complicated to get started with as well). If you need to use the module then you should check out the BeerUserMigration class in beer.inc that ships with it, read the documentation and try to create a basic working example for yourself which you can then slowly expand to get the necessary end-result. – Madis Mar 16 '12 at 21:35

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.