User settings mechanism for Yii
I need some help with user settings mechanism for my Yii-based application.
I've created the following db structure to store user settings:
table
user
with the following fieldsid | username | email | etc.
table
settingslist
(to store a list of all possible settings with descriptions) with the following fieldsid | code | name | description
table
settings
(to store all user settings) with the following fieldsid | userid | settingslistcode | value
Now I'm stuck with the form which allows user to change his settings. I had to deal before with the regular models (i.e. for posts, comments, etc.) where every new model had only one row in the database (Post model - id | title | body |
) with the certain amount of attributes (fields of the table). But now I need to store user settings in 10-15 rows and I don't know how to apply Yii model mechanism to work with this, so I can retrieve those settings in a single form (so user could change his preferences).
Any suggestions are gre开发者_C百科atly appreciated.
Thank you!
Not sure what the criteria are for your form, but you can pull all of the settings for a particular user with the following.
$criteria=new CDbCriteria;
$criteria->condition="id=".$user->id;
$settings=Settings::model()->findAll($criteria);
Then loop through each of these building your form.
foreach($settings as $setting){
$criteria->condition="code=".$settings->settingslistcode;
$settingElement = SettingsList::model()->find($criteria);
...
}
Well, I m not sure if you store user setting for his profile in your site or per post/comment.
The setting list is OK, we need to define all the possible setting options. Then, if you store site settings for your users, try a column ProfileSettings in tbl users. I usually use serialize($setting_array()).
If you store user setting per post, create a user_post_settings table and user same serialize() idea. So, one user - one post => one row.
精彩评论