[drupal-devel] Storing arbitrary data for a user
I have a small amount of persistant data I would like to associate with a user and would like to avoid creating a new table to store it in. I have come across variable_get and variable_set - which is great - but probably not appropriate for what I'm doing. I've also seen user->categories, but can't find any doco or info on what this is. Thanks, Dan
Dan Robinson wrote:
I have a small amount of persistant data I would like to associate with a user and would like to avoid creating a new table to store it in. I have come across variable_get and variable_set - which is great - but probably not appropriate for what I'm doing. I've also seen user->categories, but can't find any doco or info on what this is.
Thanks,
Dan
Try the $user->data array. You should be able to insert your user data there. To see what's currently stored there (as serialized data): select uid, name, data from users; -Mark
Mark wrote:
Dan Robinson wrote:
I have a small amount of persistant data I would like to associate with a user and would like to avoid creating a new table to store it in. I have come across variable_get and variable_set - which is great - but probably not appropriate for what I'm doing. I've also seen user->categories, but can't find any doco or info on what this is.
Thanks,
Dan
Try the $user->data array. You should be able to insert your user data there.
To see what's currently stored there (as serialized data): select uid, name, data from users;
Custom $user fields will already be saved without modifying ->data directly. In fact, custom fields that do not go into data need to be unset explicitly in hook_user('save') to prevent this. Steven Wittens
Steven Wittens wrote:
Mark wrote:
Dan Robinson wrote:
I have a small amount of persistant data I would like to associate with a user and would like to avoid creating a new table to store it in. I have come across variable_get and variable_set - which is great - but probably not appropriate for what I'm doing. I've also seen user->categories, but can't find any doco or info on what this is.
Thanks,
Dan
Try the $user->data array. You should be able to insert your user data there.
To see what's currently stored there (as serialized data): select uid, name, data from users;
Custom $user fields will already be saved without modifying ->data directly. In fact, custom fields that do not go into data need to be unset explicitly in hook_user('save') to prevent this.
I'm kind of getting this, but not quite. How do I declare a customer $user field? I'm already using the hook_user stuff so I know "when" to do it, but I still don't know where to put this data. Thanks, Dan
Dan - all you need to do is set $user with new properties during hook_user('form') or hook_user('validate'). They will be saved automatically by user.module. So if you want to set a gender, just do this global $user; $user->gender = 'female'; -moshe On Feb 19, 2005, at 3:58 AM, Dan Robinson wrote:
Steven Wittens wrote:
Mark wrote:
Dan Robinson wrote:
I have a small amount of persistant data I would like to associate with a user and would like to avoid creating a new table to store it in. I have come across variable_get and variable_set - which is great - but probably not appropriate for what I'm doing. I've also seen user->categories, but can't find any doco or info on what this is.
Thanks,
Dan
Try the $user->data array. You should be able to insert your user data there.
To see what's currently stored there (as serialized data): select uid, name, data from users;
Custom $user fields will already be saved without modifying ->data directly. In fact, custom fields that do not go into data need to be unset explicitly in hook_user('save') to prevent this.
I'm kind of getting this, but not quite. How do I declare a customer $user field? I'm already using the hook_user stuff so I know "when" to do it, but I still don't know where to put this data.
Thanks,
Dan
ok - I'm almost there.... Everything is working - except i'm working in a callback specified in <mymodule>_menu - function forummail_menu($may_cache) { global $user; $items = array(); if ($may_cache) { $items[] = array('path' => "user/$user->uid/forummail", 'title' => t('my forum mail settings'),'callback' => forummail_page, 'type' => MENU_LOCAL_TASK); } return $items; } this sets up a "tab" in the user/2/forummail. So now I have a form and everything is working - however I'm not hooking 'form' or 'validate' so it is not saving the user. I've tried to manually call user_save - but it doesn't like that very much. Here is the code I'm trying to execute - function forummail_page() { ..snip.. if ($edit['saved']) { // we're back from a save form event $user->forummail_mode = $edit['mode']; // save off the data here!! } ..snip.. } Thanks, Dan
Dan - all you need to do is set $user with new properties during hook_user('form') or hook_user('validate'). They will be saved automatically by user.module. So if you want to set a gender, just do this
global $user; $user->gender = 'female';
user_save() is a bit strange. Just keep playing with it and you'll get it. you have the right direction. You could also grep the Contrib repository for examples. note that your example menu item should go into the !$may_cache section since it has a userID in it. If you don't do so, you might confuse an admin working on the admin/menu page. On Feb 20, 2005, at 1:13 AM, Dan Robinson wrote:
ok - I'm almost there....
Everything is working - except i'm working in a callback specified in <mymodule>_menu -
function forummail_menu($may_cache) { global $user;
$items = array();
if ($may_cache) { $items[] = array('path' => "user/$user->uid/forummail", 'title' => t('my forum mail settings'),'callback' => forummail_page, 'type' => MENU_LOCAL_TASK); }
return $items; }
this sets up a "tab" in the user/2/forummail. So now I have a form and everything is working - however I'm not hooking 'form' or 'validate' so it is not saving the user. I've tried to manually call user_save - but it doesn't like that very much. Here is the code I'm trying to execute -
function forummail_page() { ..snip..
if ($edit['saved']) { // we're back from a save form event $user->forummail_mode = $edit['mode']; // save off the data here!! }
..snip..
}
Thanks,
Dan
Dan - all you need to do is set $user with new properties during hook_user('form') or hook_user('validate'). They will be saved automatically by user.module. So if you want to set a gender, just do this
global $user; $user->gender = 'female';
One more reason not to respond to questions when I'm too sleepy to do it right ;)
Custom $user fields will already be saved without modifying ->data directly. In fact, custom fields that do not go into data need to be unset explicitly in hook_user('save') to prevent this.
Steven Wittens
participants (4)
-
Dan Robinson -
Mark -
Moshe Weitzman -
Steven Wittens