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.

Database: User table vs UserMeta table

I've searched the forums and the documentation but I can't find an answer to my question:

  • When I add coloumns to the User table with the same name as in the UserMeta table then "the model takes care of the rest".

How do I retrieve this info ? I'd like to extend the MemberlistEnhanced plugin with info from the ProfileExtender plugin. The extra profile fields are stored in UserMeta (i.e. Profile.Hometown, Profile.Country etc).

Comments

  • x00x00 MVP
    edited November 2014

    Individually

    $ProfileInfo = UserModel:GetMeta($UserID, 'Profile.%', 'Profile.');
    

    If you have a bunch of users this may not be too efficient. That is one of the advantages to serialising in the User table.

    The simplest thing you might do is collect all the UserID. Don't bother with JOIN as transposing the rows will be a major hassle. Then select all that profile info with those userid

    $UserMetas = Gdn::SQL()->Select('um.*')->From('UserMeta um')->Where('um.Name like', 'Profile.%')->WhereIn('um.UserID',$UserIDs)->Get();
    

    Loop through all those storing in a lookup array the profile data per UserID, then you can zip it to the User.

    grep is your friend.

  • R_JR_J Ex-Fanboy Munich Admin

    The GetMeta function accepts an array for the parameter UserID: https://github.com/vanilla/vanilla/blob/2.1/applications/dashboard/models/class.usermodel.php#L1127-1131

    So if you already have $Users, you can do $UserIDs = ConsolidateArrayValuesByKey($Users, 'UserID'); and use that array with UserModel::GetMeta

  • @R_J but this is not super efficient as it is fetching each individually.

    grep is your friend.

  • R_JR_J Ex-Fanboy Munich Admin

    Don't think so: https://github.com/vanilla/vanilla/blob/2.1/applications/dashboard/models/class.usermodel.php#L1137

     if (is_array($UserID))
        $Sql->WhereIn('u.UserID', $UserID);
    else
        $Sql->Where('u.UserID', $UserID);
    
  • you are indeed correct I was looking at UserMetaModel code.

    grep is your friend.

  • Thanks for all the answers/suggestions. I'll try and add it to the MemberlistEnhanced plugin.

  • I've tried to adjust the MemberlistEnhanced plugin to have it display the extra profile fields. No success so far. I guess I need some more pointers:

    Where do I add the code to retrieve the profilefields ? In the class..,php or in the view ? Is there a simple plugin I could use as an example ? Thanks !

  • R_JR_J Ex-Fanboy Munich Admin

    Maybe you can show us what you've tried so far and we will be able to correct your code.

    If you get something from the database it shouldn't be done in the view. I don't know if that plugin has a model where that should be done in the MVC scheme, but if not, I'd suggest you fetch the data in the plugin file and pass it to the view like that $Sender->SetData("SomeReferrer", $Data);

Sign In or Register to comment.