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

Fetch associated data for views

Discussion in 'XenForo Development Discussions' started by Robust, Sep 25, 2016.

  1. Robust

    Robust Well-Known Member

    So I save groups for my add-on as IDs (for example, the tag 'moderator' may be ID 2). In the database this is ID 2, the tag is saved in the table myaddon_groups.

    The tag should be shown on profiles, posts, etc. So I need to go from ID to the tag. What's the most efficient way to extend XenForo's code to show the tag instead of the ID?

    This tag is shown mainly in threads, posts and profiles, but I don't want to limit to that if possible.
  2. Robust

    Robust Well-Known Member

    A little urgent. Hoping for a good method to do this ASAP please.
  3. Chris D

    Chris D XenForo Developer Staff Member

    It's not really clear from what you've said what you're aiming to do. There's not really a lot of context as to what a "tag" is. Is it just a word like a prefix?

    If so, then you could use the same approach which is also generally more flexible - use phrases.

    So, you would just save a phrase, e.g. youraddon_tag_2 with text "Moderator".

    If this is globally cached there's no query cost.

    Depending on the exact use you might need to use a template helper which just takes the ID and returns the correct phrase object.
  4. Robust

    Robust Well-Known Member

    A bit like that, but phrases aren't possible because the text association is dynamic.

    The best way to think of this system is like the user groups system.

    There are groups, like Administrative, Moderating, Registered. These group have IDs, 1, 2, 3. There is User 1. He is in group 2 and 3. In the database, this is saved as 2 and 3. These groups are shown on posts, though. {$user.myaddon_groups} will return "2,3". {$user.myaddon_group} (their primary, display group) is "2" in this case. Obviously, I don't want to show the text "2,3" or "2", respectively, I want to show "Moderating" and "Registered", or "Moderating" if just showing their primary group.

    The reason phrases won't work is that these groups are made by the forum admin, they can change the names as they desire, etc. This data is stored in the table myaddon_groups under the field "title".

    I need to fetch this title with the IDs somehow.
  5. Robust

    Robust Well-Known Member

    Sorry to be a pain, but any idea here?
  6. Chris D

    Chris D XenForo Developer Staff Member

    I honestly don't really have any other ideas based on what you have said so far.
  7. Robust

    Robust Well-Known Member

    Is the way I explained it unclear (it seems confusing when I read it over kinda)? If so, simpliest way for me to put it is how I can fetch the name of a 'group' from its unique ID, having to do this for everywhere it is displayed.

    I mean, for example, if I had to show the name of the user's primary usergroup on all their posts, how would you fetch the name from the ID (from user_group_id) in those views?
  8. Chris D

    Chris D XenForo Developer Staff Member

    Possibly another idea that might not be workable, but if it was user groups, where there might not be a very large number of them, you could just cache them all in the XF simple cache.

    You'd then look up the ID against that cache.

    If that's no good, then you'd need some way of enumerating all of the IDs on each page load, do a single query per page to fetch the titles needed, and then use that.
    Robust likes this.
  9. Robust

    Robust Well-Known Member

    Since it's on the user, how about editing the fetch options for a XenForo user and throwing in a join to select the title from the groups table? How efficient would that idea be?

    SimpleCache would also work in my scenario, just never seen it used before in this kind of case. Shouldn't really be more than 20 groups, generally around 10.
  10. Chris D

    Chris D XenForo Developer Staff Member

    Usually easier to read from the cache than it is to faff around with joins.

    XenForo_Application::setSimpleCacheData('key''foo'); // set simple cache data under the key 'key'
    $foo XenForo_Application::getSimpleCacheData('key'); // get simple cache data for the key 'key'
    XenForo_Application::setSimpleCacheData('key'false); // delete simple cache data for the key 'key'
    Robust likes this.
  11. Robust

    Robust Well-Known Member

    Big thanks @Chris D. I'll try that method out, it should work better assuming cached data doesn't somehow get deleted.
  12. Chris D

    Chris D XenForo Developer Staff Member

    The cache should be permanent; just try to use a relatively unique key name (to your add-on) to ensure no other add-on accidentally overwrites or delete it. FYI the simple cache is actually stored under the simpleCache key of the xf_data_registry.
    Robust likes this.
  13. Robust

    Robust Well-Known Member

    I need to add that data into templates, so I can use the response params to add that cached data to be accessible in the relevant templates, but how do I dynamically fetch from it?

    Say I use {$myAddonGroups} and the user's group is {$user.myAddon_group_id}, it's invalid to do a {$myAddonGroups.{$user.myAddon_group_id}.name} so how do I go about doing that part?
  14. Chris D

    Chris D XenForo Developer Staff Member

    The simple cache is available to all templates automatically by the way.


    That should do it.
    Robust likes this.
  15. Snog

    Snog Well-Known Member

    Please keep in mind that the simple cache is shared by all add-ons and there is a limit to it's size.

Share This Page