I might be approaching the problem from the wrong point of view so please correct me if my technique is wrong.
I’m creating guest users on a website with php. When someone visits the website, a guest user is automatically created. But if that person logs into a “normal” account (non guest) then whatever that person did in the guest account is transferred to the new account.
Example, someone visits a website without authenticating, add products from the shop in his cart and then logs in. I will need to transfer the cart items from the old user (guest) to the new user.
But it would be too time consuming (and possibly too resource hungry) to find every item in every table that is linked to the guest user account and then change them all. Right?
But I’ve got constraints! Yay! If I update the guest user id, all the constraints change. Fabulous! But what if I need to assign them to another user id? How would I do that?
Any help is greatly appreciated.
You can’t do exactly what you’re suggesting because the user row (for the logged in user) presumably already exists. The only option would be to go through each table and update the UserID there, like you suggest. The feasability of this depends on what they can do as a guest user. If it’s only one or two tables, just do it this way.
But, every problem can always be solved by another layer of abstraction, right? Put another single table (I’ll call it “pseudousers”) in between the real users table and everything else. Link everything else to this new table, and use the “psuedousers” to link up to the users table. When the guest user logs in, just change the UserID in the pseudousers table and you’re done.
What I mean is:
+---------+ +-----------+ +-------+ | Users |----|Pseudousers|----| Carts | +---------+ +-----------+ +-------+ | | +-------+ |------------| ... | +-------+When you create a guest user, you create a new row in Users and Pseudousers. When they log in, update the UserID in Pseudousers to point to that user, and then you can safely delete the guest user row in Users (it’s no longer in use).
Hopefully someone can come up with a better name than “Pseudousers” though.