PDA

View Full Version : Updating an existing user


richever
05-28-2008, 07:33 PM
I am trying to figure out what I need to do in order to user the User datamanager to update an existing user.

Most of the examples that I could dig up had to do with creating and saving a new user, but I'd like to update a user. Here's what I'm presently doing:


$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$userdata->condition = ('userid = ' . $userid);
$userdata->set('userid', $userid);
$userdata->set('email', $email);
$userdata->set('password', $password);
$userdata->set('username', $username);
$userdata->save();


When I try this for a user that already exists I get the following error message: 'That username is already in use or does not meet the administrator's standards...'

I thought that setting $userdata->condition would signal that an update needs to be done. Looking at the code for the User datamanager it would seem that condition needs to be set to something anyway.

Any ideas on what I'm missing here?

Thanks!

Rich

--------------- Added 1212012154 at 1212012154 ---------------

OK, I think I figured it out. I got something working for me anyway but so I thought I'd share.

For some reason providing the username when an update is desired seems to have been causing the problem I described in my previous post. Here is my code now:


$userid = $this->getUserId($username);
$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);

if ($userid > 0)
{
$userdata->condition = ('userid = ' . $userid);
$userdata->set('userid', $userid);
}
else
{
$userdata->set('username', $username);
}

$userdata->set('email', $email);
$userdata->set('password', $password);
$userdata->save();


If there is a better, perhaps more accepted, way of doing this then please let me know.

Rich

--------------- Added 1212012297 at 1212012297 ---------------

One more thing. getUserId() is my own convenience function. It looks like this:


// Userid for username. 0 if the user is not found.
public function getUserId($username)
{
global $vbulletin;

if (empty($username))
{
return 0; // or whatever error you deem appropriate
}

$user = $vbulletin->db->query_first("
SELECT userid FROM " . TABLE_PREFIX . "user
WHERE username = '" . addslashes(utf8_decode($username)) . "'");

return $user ? $user['userid'] : 0;

}

Opserty
05-28-2008, 09:57 PM
Well I haven't had time to read through all your thread but here is how I would do it:


$username = 'blabla';

// Optional... or you can use your function
$userid = fetch_userid_from_username($username); // You will need to copy this function from usergroup.php in order to use it


$userinfo = fetch_userinfo($userid);

$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_ARRAY);
$userdata->set_existing($userinfo);
$userdata->set('email', $email);
$userdata->set('password', $password);
$userdata->save();

Something along those lines.