1

Is it possible to use the saveUser method to import users with an existing ID?

craft()->users->saveUser($userModel)

I need to import 2000+ users and when doing so, I need to make sure that their old ID doesn't change after inserted, because I need to import other tables too and match them based on that.

The excellent import plugin tries to update the users instead of creating new ones when I tell it to import the ID as well. I'm not sure if that's intended behavior or a bug.

1

If the UserModel contains a user ID, then the existing user will be updated.

If the UserModel does not contain a user ID, then a new user will be created.

Either way, the UserModel will contain an ID after being saved. Since models in PHP are passed by reference, the ID will get set automatically for new users.

  • Thanks for the reply, but that's not the question. I want a newly created user to have exactly the same ID as it was in the previous database. – Tibor Szasz Sep 29 '16 at 18:30
  • Ah, that was not clear in the original question. – Lindsey D Sep 29 '16 at 19:13
  • And if I guess right it's not doable? – Tibor Szasz Sep 29 '16 at 19:14
  • It's probably doable, but complicated. I'll let someone else take a shot at answering that. – Lindsey D Sep 29 '16 at 19:15
  • I think I'll do a surgical fix: add back the new user ID-s to the old tables and export them with the fresh ID-s :) Ugly, but only needs to be done once. – Tibor Szasz Sep 29 '16 at 19:18

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.