Saving User Settings on a controller-action-basis

Hi folks,

i’ll post some code and code snippets i’ve used in my last projects. Maybe this could help or inspire some of you - don’t know :slight_smile:

I don’t claim that this is best programming practice nor best programming practice for yii - it just is what it is!

Starting with this pretty simple extension which i’m using to save any kind of “page based user settings” in database like “show X entries per page”, “last visited page in pagination was X”, “selected date format for XY is dd#mm#yy” and stuff like that.

Btw. all code was written for/with Yii 1.0, there could be differences to 1.1

Therefore i’ve set up two tables storing the setting items and their values:

[sql]

CREATE TABLE IF NOT EXISTS user_settings_assignment (

ItemID int(11) NOT NULL,

UserID int(11) NOT NULL,

Data varchar(255) DEFAULT NULL,

PRIMARY KEY (ItemID,UserID),

KEY UserID (UserID)

);

CREATE TABLE IF NOT EXISTS user_settings_item (

ID int(11) NOT NULL AUTO_INCREMENT,

Type int(11) NOT NULL DEFAULT ‘0’,

Item varchar(255) NOT NULL,

PRIMARY KEY (ID)

);[/sql]

You can find the extension file being attached to this post.

So, what does it do?

Well, it simply stores items (per default they look like “controllerID_actionID_mySettingPropertyName”) inside ‘user_settings_item’. Sidenote: the ‘Type’ field isn’t used yet, i’ve added this to extend it for different setting types.

The value for an item in context of the logged-in user is then stored in ‘user_settings_assignment’. Simple.

How to use

Let’s start from scratch.

  • Install: create a folder inside ‘/protected/extensions’ and name it e.g ‘usersettings’. Afterwards copy the EUserSettings.php into that folder.
  • Setup SQL: add the two tables from above to your database (sql-script also included in attached zip file)
  • Setup Yii: to use yiis nice autoloading feature goto your '/protected/config/main.php ’ and add ‘application.extensions.usersettings.*’ to the ‘import’-array.

Now, how to use the extension:

Rudimentary way: let’s say you’re having a form with an “Show entries per page”-field inside an admin-view, where you let the users decide how many entries they would like to see per page. This form is submitted using post-method.

Inside your controller action ‘actionAdmin’ you add the following code:


 public function actionAdmin() {

/* ... */

$userSetting = new EUserSettings(); // Create UserSettings object

if(isset($_POST['entriesPerPage']))

    $userSetting->entriesPerPage= $_POST['entriesPerPage']; // Saving entered 'entriesPerPage' to database


    if($userSetting->entriesPerPage > 100000) { 

        // Just a stupid example that shows you could get the value as if it's

        // a class property. 

        echo 'Are you nuts?';

    }

/* ... */

}



That’s it? That’s it!

What is happing? Well, first of all the EUserSettings class uses the magic php methods __get and __set, so you can read and write your user settings as if they were class properties. Second: per default you don’t need to create setting items by yourself, when you set the value of an setting item it’s created automatically for you if it doesn’t already exist. It uses the called controller, the called action inside this controller and the id of the logged-in user to create an item and the value entry.

ATTENTION

I’m using numerical User IDs in my projects. But the default web application generated with yiic uses strings (the username) as id. So if you’re using a string as a User ID you need to change the SQL for creating the table ‘user_settings_assignment’ accordingly!

The hole class is coded pretty much straight forward so i think it isn’t hard to understand, but if something is not clear or you’re having a better idea of doing things, you’re welcome to post! :slight_smile:

Best regards,

yoshi

[b]

P.S.: Maybe some mod looks inside here: I can’t attach any file and the message “This upload failed” is very informative![/b]

Nevertheless you can download the code here: usersettings.zip

Sounds good. Why not upload it here? :P

Well, to be honest i would like to get some more reviews and tests before adding something to the extension library.

Because a big bunch of crappy extensions isn’t very helpful at all, is it?!

So, maybe you would like to take a look at it and speak out ;)

Well I think it’s fine to add your extension if it works at least. If you’re not sure that you did everything the “best” way, just wait for feedback here or in the extension area. You can upload a “better” version any time.

But I will take a look tomorrow anyway and then give some feedback.

Seems like no one needs something like this - because i used a wrong version of the php file there was a bug inside which made it impossible to read out the previous saved settings, but no one was yelling :D

Nevertheless i’ve update the download archive in my first post.