I need to send some notification emails to the single users, the user should be able to change his preferences on what tipe of emails he wants to receive.
I think I can store the user preferences in the database but I need to set up all the preferences to yes as default, so maybe I could save all the preferences in the database when I create the user but what happen if in the future I decide to add more preferences? What is the best way to achieve this? Many thanks
This is just a question of how you want to store the preferences. If you set a simple field to true/false if they are able to change their preferences, that is limiting for sure. However, if you think you might want to store a ton of options, you could store something like a json object in a text field. This is one way many CMS systems do it.
When you want to then read the option back out, you read the JSON text from the database, use your server side language (like PHP) to convert that JSON to an object and start checking for various options.
If you want to change or add an option, you read the database, convert to an object, set its various options, then save the object back to the database. This isnāt the only way to do it, but is a simple one to implement.
If you would like to know more about this idea, check out the idea of āserializationā (or the process of converting classes/objects to text and back.
Another option is much like how WordPress manages its options where you have a simple table that has a series of keys => value pairs that are tied to a user ID. Each key is for an option like āchange_preferencesā => true. The one draw back to this is if you start scaling it up to hundreds of thousands of users and each user has like 20-30 pairs, you have a lot of records and many of them repeat other than the user ID.
I am of the opinion that using a database JSON field that stores a userās preferences as the way to go. Even adding on to the object later is really easy. You can test if a userās JSON has an option, if not, you add it and resave to the database.
Hi many thanks for your answer. So if I save the json with all the options when I create the user then I can set all the preferences as true (so by default every user will receive the notification) then when the user changes the options I will update the json in the database. The only problem is if in the future I want to add more preferences will be difficult to set the new one as default for each user that has already been created in the past.
In the WordPress option on the other hand will be easier to query the database with mysql if I need to select all the users with a certain preference set to yes
Hi guys many thanks. Even if I like @Martyr2 json suggestion I believe Iāll go with @igor_g suggestion. Then if in the future I add more settings Iāll notify the user about this new option and the possibility for them to activate it from profile preferences as I believe it will be complicated to add the new option in the join table for existing users.
Well as mentioned before, that is one way to do it but if you are expecting a large number of users be prepared for exponential number of rows. Lets say you have 100k users. Then users-to-preferences will have a 100k new rows for each preference you add.
It has some advantages for sure, easy searching and querying through the join table, just be aware of that fact.
Well off the top of my head I know that WordPress does this for certain options. Not necessarily JSON, but they use PHP serialization for some of the roles/permission fields. They also serialize arrays etc. I think Joomla use to do something similar back in the day. I donāt know if they still do it now.
Many WordPress plugins also do it for storing their options. One option in the options/options meta tables and they can store their entire pluginās settings. Easily retrieve and set it through get_option and update_option methods.
Be very careful about assuming opt-out with usersā preferences for receiving emails from your site. Depending somewhat on what you mean by ānotification emailsā, you may run afoul of several countriesā anti-spam laws.