Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Quick and easy way to retrieve user name via the id?

zodiacdmzodiacdm
edited February 2012 in Vanilla 2.0 - 2.8

Just wondering if anyone knew a trick to retrieve a user's name when all you have is the ID number, besides creating a new user model and retrieving all of that data. I.E. where it displays the user name in forum threads.

Best Answers

  • x00x00 MVP
    edited February 2012 Answer ✓

    This is not an efficient way of doing things. You are making one query, per person.

    Fortunately the user model already has a way of getting all the user in a model, and zipping the result set. This is like a denormalised join, as joins have overhead too.

    if $this->Projects is already of class Gnd_ResultSet thn you are good to go otherwise this->Projects = new Gnd_ResultSet(this->Projects);

    then what you need to is try

     Gdn::UserModel()->JoinUsers($this->Projects,array('InsertUserID'));
    

    grep is your friend.

  • x00x00 MVP
    Answer ✓

    Yep that is becuase you can join muliple user meta say you had $Project->InsertUserID and $Project->MentorUserID for example you would put:

     Gdn::UserModel()->JoinUsers($this->Projects,array('InsertUserID','MentorUserID'));
    

    grep is your friend.

Answers

  • jspautschjspautsch ✭✭✭
    edited February 2012

    That's what the user model is for...

    You could just query the database directly:

    Gdn::SQL()->Select('Name')
    ->From('User')
    ->Where('UserID',$UserID)
    ->Get();

    But really why not just use the user model?

    If you want to learn more about how Vanilla works, check out the Vanilla Wiki

  • x00x00 MVP
    edited February 2012

    ???

    the query is made through the model anyway. You can use GetID from static:

     $User = Gdn::UserModel()->GetID($ID);
    

    grep is your friend.

  • x00 said:
    ???

    the query is made through the model anyway. You can use GetID from static:

     $User = Gdn::UserModel()->GetID($ID);
    

    Yeah exactly

  • Sorry, yea, kinda dumb question.

    Just looking for the most practical way to achieve what I am trying to do here.

    I would be making calls for multiple user's names in a 'foreach' statement in a view this way, just seemed like there should be a more practical way than to create the same object multiple times.

    I'm pretty much self-taught, so my methods are probably anything but orthodox.

    Essentially what I believe I need is a mysql join in my model, though the use of joins confuse me.

    Let's say we have table ExampleA of data, the InsertUserID being one of the columns.

    What would be the correct syntax to retrieve data from table ExampleA and join it with the table User to get insert user name with the rest of the data from ExampleA ?

    How would I then access this data?

    For each $Datas as $Data

    $Data->ID $Data->InsertUserID $Data->User->Name

    ?

  • In the meantime, I have added the following in my controller;

    foreach($this->Projects as &$Project) {
    $Project->User = Gdn::UserModel()->GetID($Project->InsertUserID);
    }

    Would there be a benefit to using join over this?

  • Might help to know more how your code works. How is the $Projects data generated in the first place?

  • x00x00 MVP
    edited February 2012 Answer ✓

    This is not an efficient way of doing things. You are making one query, per person.

    Fortunately the user model already has a way of getting all the user in a model, and zipping the result set. This is like a denormalised join, as joins have overhead too.

    if $this->Projects is already of class Gnd_ResultSet thn you are good to go otherwise this->Projects = new Gnd_ResultSet(this->Projects);

    then what you need to is try

     Gdn::UserModel()->JoinUsers($this->Projects,array('InsertUserID'));
    

    grep is your friend.

  • Learn something new every day

  • It is a good idea to study the models. There is plenty of useful stuff already there.

    User table is always being used in a classic 1=1 relationship, so it makes sense to have a denormalized join method.

    grep is your friend.

  • zodiacdmzodiacdm
    edited February 2012

    Thanks, It is of type Gdn_ResultSet. I used a standard get function in my model. $Data = $MyModel->Get();

    but how do I reference the user name in the view now?

    It was $Project->User->Name the way I had it before. I am studying the user model and have not figured this out yet...

  • zodiacdmzodiacdm
    edited February 2012

    zodiacdm said:
    Thanks, It is of type Gdn_ResultSet. I used a standard get function in my model. $Data = $MyModel->Get();

    but how do I reference the user name in the view now?

    It was $Project->User->Name the way I had it before. I am studying the user model and have not figured this out yet...

    Got it. It adds an 'Insert' Prefix to the fields of your choosing, or 'Name', 'Photo', and 'Email' by default.

    So it is now $Project->InsertName or $Project->InsertEmail

  • x00x00 MVP
    Answer ✓

    Yep that is becuase you can join muliple user meta say you had $Project->InsertUserID and $Project->MentorUserID for example you would put:

     Gdn::UserModel()->JoinUsers($this->Projects,array('InsertUserID','MentorUserID'));
    

    grep is your friend.

  • Still early in development, but.... :)

  • Thanks for that last comment, too, Knowing I can do this for multiple users is extremely useful.

    Eventually I would like projects to be based on individuals, and allow for the creators to invite other users, allow public sign-ups, etc.

    This type of application normally requires a monthly fee... I'm trying to beat that, and get exactly what I wanted in the first place.

Sign In or Register to comment.