How should I handle entries when the user is deleted? - sql

I run a forum which I built myself. Ok so all users have the opportunity to delete themselves.
But all their threads and posts will remain. But right now where it should say their username it's just blank.
How should I handle this?
Should I make a new user and call it e.g. "deleted user" and assign all threads/posts to that ID when they delete themselves?
Or should I just check if the user ID exist if not print e.g. "deleted user" as username?
What's the smartest way? Any other ways tell me.
Thanks!
p.s (i'm not a native english speaker, looked up some fancy words on a online dictionary)

I would suggest not actually deleting the user. You could simply add a column to the users table such as:
ALTER TABLE users ADD COLUMN (is_active TINYINT(1) NOT NULL DEFAULT 1);
Then when you "delete" a user, simply mark them as inactive:
UPDATE users SET is_active = 0 WHERE users.id = 7;
For user listings, and account access you would check the is_active status. For displaying of data such as posts and what not, you'd not care about their active status, youd just grab the name from the table.

Related

Wordpress SQL request to delete all user subscriber without publication

I have 3000 users and 2800 user without publication, so i want to delete all spam user with sql. thanks
Your question isn't very clear -
But if you mean "delete all users who have never posted anything" - Then
DELETE FROM `wp_users`
WHERE ID NOT IN
(SELECT DISTINCT post_author FROM `wp_posts`);
would do it... remember to back up your data first.
This will run through all the posts, make a list of the user IDs that ARE authors and then delete anyone who isn’t on the list from your wordpress users. Since custom post types are stored in wp_posts, it’ll be checking the author against all of those too.

Laravel get a random user with where clause

My table schema is a id field, name field and a friends id field.
Every user must have a maximum of 2 friends. When a new user is created and event is fired and a listener which listens to the created user event then adds a random friend to a newly created user.
$randomfriend = DB::table('users')->select('id')
->groupBy('friends_id')
->havingRAW('COUNT(*) < 2')
->inRandomOrder()->first();
it still returns users with maximum number of friends. Can someone help me with this?
first of all create another table for friends relations.
for selecting a random user this post can help you:
enter link description here
don't forget to put this code in while loop for checks.

Messages read by groups without own database table

I have this table used when a user writes a note.
When writing a note, the user specifies if a sell-department and/or a buy-department should receive the note.
Each user can create a Case (lets say its just a table with case_id and case_text). And the notes the users write are related to a case.
So the table NOTES is (postgres database) something like this:
ID
TEXT (the message itself)
USER_ID (the user that writes the note)
CASE_ID (the case_id for which the note is been written)
Short word about users:
There are "ordinary" users and those working on a department. This should not make big difference for the description here.
When an ordinary user writes a note, let's say he want both buy-department and sell-department to be included (being informed about the note/see the note).
What happens now is that there is another table called UserNotes. It looks like this:
ID
IS_READ
NOTE_ID
USER_ID
DEPARTMENT_ID
READ_AT_DATE
So ordinary user with id = 1 writes this note and in the code (as he tells sell and buy-departments have to be included) I search for all the users working at that specific sell-department and all those working at that specific buy-department. I then put all these users in the table UserNotes. With IS_READ false by default.
When a user in the specific sell-department reads the note, I will then change IS_READ for this user's entry in UserNotes.
This is how it works today. I don't think this is scalable. I'm already getting performance issues. I don't think it is important to know when a note has been read. So because of this I was thinking that maybe the following solution could work. Please have a look and tell me if it could be better and the current one or if you have some other suggestion please let me know:
I drop UserNotes table. I add a new filed in table Notes: READ_BY. Here I will update the field each time a user reads the note.
I don't know if I could use some postgres-specific thing, maybe making this field a json-string and searchable.

Podio External ID change

We use an external setup that sends information on our orders into Podio and from there we manage them. We have had an issue with our Email field and it's lead to the External ID 'email' being lost (we are up to email-6' now before realising the issue). Is it possible to reset/restore the External ID to be 'Email' instead of 'Email-6' and if so could you please point to how we can do it. The developers we use don't work with us anymore so it's a bit of a nightmare to sort out. We are reasonably tech savvy but not wiz kids by any stretch of the imagination. I've copied below what our old developers said:
"What I'm pretty sure is your case, is that you have deleted the original Email field from the template, and now you are trying to put it back. The problem is that, only by appearance, you have a single Email field in your template, but when Fabnami is sending the data, the collected customer email is attached to the field with external ID "email", not the one you have only labeled as Email. The result is that podio will exhume the deleted field and will display it using the last labels it has. In your case there will be the original field with label "Email" and ExternalID "email" plus a second empty field with label "Email" but different External ID."
I hope you can help :)
Each time, when the field get created, external_id get created in parallel(1 to 1 mapping). If you delete the field the external id get deleted along with that. You can't reset the external id "email" to "email-6".
There is a option to recover the first deleted field "Email" -> external is id "email", but with the recovery payment.
Each time, when the field get created, external_id get created in parallel. If you delete the field the external id get deleted along with that. You can't reset the external id "email" to "email-6".
There is a option to recover the first deleted field "Email" -> external is id "email", but with the recovery payment.

How to check current_user is present in particular recordset?

I have Three tables User,Event and EventInvitee.
A user creates an event and invites other users in that event.
Now using below code I get all the users invited in a particular event.
#event=Event.find(4)
#event.event_invitees
but now I want to check that current_user is present in #event.event_invitees?
How can I do that?
fields of EventInvitee table are id,user_id,event_id and relationship is defined properly among those three table.
please help me.
thanks in advance.
You can check that current_user is exists in selected EventInvite's use exists? method:
#event.event_invitees.exists?(user_id: current_user.id)