I was wondering if there is a widely accepted way to store a user’s preferences in a CakePHP application. For example, some ways I can think of are:
- Having individual settings in the user model as fields.
- Having a preferences table with a key and value field.
- Having a preferences table with one entry per user and preferences as fields. (Similar to the first, I suppose)
Is there a way that might be better for an application that might change or expand (i.e. one that may account for preferences to be added or removed)?
You might want to look at the KeyvalueBehavior in CakeDC’s Utils plugin as a starting point.
Extract the plugin to
Plugin/Utils/and create a database table calledpreferenceswith the following fields:user_id,field,valueYou can then save and retrieve preferences like so (assuming
User hasMany Preferences):