Fetch associated data for views

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.
 
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.
 
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.
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.
 
I honestly don't really have any other ideas based on what you have said so far.
 
I honestly don't really have any other ideas based on what you have said so far.
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?
 
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.
 
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.
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.
 
Usually easier to read from the cache than it is to faff around with joins.

PHP:
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'
 
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.
 
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.
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?
 
The simple cache is available to all templates automatically by the way.

{$xenCache.yourCacheKey.{$user.myAddon_group_id}.name}

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