1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Best practice for getting user ID out of a user model object...?

Discussion in 'XenForo Development Discussions' started by DeltaHF, Apr 22, 2014.

  1. DeltaHF

    DeltaHF Well-Known Member

    This feels like a really stupid question, but here goes:

    My add-on gets a user name from the URL, and I want to convert that to an ID. It seems easy enough, but...
    PHP:
    if (!empty($_GET['username']))
    {
        
    $user $this->getModelFromCache('XenForo_Model_User');
        
    $user->getUserByName($username);

        
    /* OK...now what? */
    }
    I expected to just use some getUserId() method, but there's not one that I can see. I did find getUserIdFromUser(), but I'm not sure how that is supposed to work.

    So...what is the proper way to do this? Do I have to just extend XenForo_Model_User and write my own "getter"?
     
    Last edited: Apr 22, 2014
  2. Brogan

    Brogan XenForo Moderator Staff Member

    I haven't looked at the code but have you tried something like:
    PHP:
    $userId $this->getUserIdFromUser($user);
     
  3. DeltaHF

    DeltaHF Well-Known Member

    That brings up a "call to undefined method" error. The code above is being called from a Controller which extends XenResource_ControllerPublic_Resource, so it (as "$this") doesn't have the getUserIdFromUser() method.

    I have tried something like this:

    PHP:
    $user_id $user->getUserIdFromUser($user);
    ...but that didn't work, either. Passing an object to itself feels weird, anyway. :p
     
  4. Brogan

    Brogan XenForo Moderator Staff Member

    In which case just call the model from the controller in the usual manner.
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    PHP:
    if (!empty($_GET['username']))
    {
        
    $user $this->getModelFromCache('XenForo_Model_User');
        
    $user->getUserByName($username);

        
    /* OK...now what? */
    }
    The function names are pretty self explanatory.

    You should think about changing this:
    PHP:
    $user $this->getModelFromCache('XenForo_Model_User');
    To:
    PHP:
    $userModel $this->getModelFromCache('XenForo_Model_User');
    This is what I mean by self explanatory. "getModelFromCache('XenForo_Model_User')" is going to get you the user model. Effectively this just loads the object that gives you access to a set of functions common to performing many XenForo database actions plus those specific to users. This doesn't return you a user, therefore it's more appropriate to define this as $userModel rather than $user.

    Moving on, this is actually correct:
    PHP:
    $userModel->getUserByName($username);
    But two things. I'd recommend changing it to:
    PHP:
    $userModel->getUserByNameOrEmail($username);
    Just simply for the fact that it's possible that some users consider their e-mail address to be their username and actually logging on at your board they can use either.

    But also, you're absolutely getting the user's details, but it's not being stored in any sort of variable. It's effectively a dead end with no way of accessing it. So it would just change to:
    PHP:
    $user $userModel->getUserByNameOrEmail($username);
    $user now contains a full user record as an array which has been found from $username.

    So, how to get the user ID? Simple now:
    PHP:
    $userId $user['user_id'];
    Bringing it all together:
    PHP:
    $userModel $this->getModelFromCache('XenForo_Model_User');
    $user $userModel->getUserByNameOrEmail($username);
    $userId $user['user_id'];
     
    phalseid, Steve F, DeltaHF and 4 others like this.
  6. DeltaHF

    DeltaHF Well-Known Member

    Awesome post, Chris, that's a very helpful explanation and good advice. Thank you! (y)
     
    Chris D likes this.

Share This Page