XF 2.0 Cron: Rebuild user caches

NicolasZN

Well-known member
Hey!
Is it possible to make rebuilding user caches a cron?
I found similar threads in the past, but they seemed old enough to not be about 2.0, and never really provided a solution.
 
I don't really understand what you'd hope to gain from that? If it needed to be a cron, it would be a cron already, so if you really need to run this on a regular basis, I'd have a look into why this would be necessary in the first place, rather than implementing it as a workaround.
 
The reality is simply that I'm trying to shoehorn a legacy system coded to integrate with a now defunct forum software into working with Xenforo as quickly as possible. The quickest way to do this is to let the other system modify a single custom user field in the xenforo DB for users from time to time. Unfortunately, when it does so it can't update the user cache for that user. Which is why I'm thinking a cron might be the easiest solution.

I know it's not the way Xenforo is made, and I wish I wasn't trying to get a live community set up as quickly as possible with some of the core features they've come to rely on - but I am, and so I'm in the situation where I need to figure out how to link these two otherwise completely separate softwares together.
 
You can launch the XenForo app in your secondary system and use the entity system to update the user. That should also correctly trigger the rebuild.
 
That sounds like a good direction - definitely easier on processing power than running a large cron job with any frequency.
Is there some documentation you could point me to about how to launch the app in another php file, and about how to use entities to update the one user?
 
People around here use this snippet to launch the app externally:
PHP:
const XF_ROOT = __dir__; // change this as needed
require(XF_ROOT . '/src/XF.php');
\XF::start(XF_ROOT);
$XF = \XF::setupApp('XF\Pub\App');
$XF->start();
You'll need to make sure to only launch the App once however, otherwise you'll receive an error.

For entities & finders, check the dev docs.
 
Thanks Lukas:

I've been digging around but without too much luck, so my systems reconnecting for the community I transferred to XF has stalled for now.
I found this post (for XF 1.5) and am wondering if it's a similar process I should follow to update the cache of a single user in XF 2 - and it seems like that post is rebuilding their permissions, is the function to recache their profile fields distinct?
 
The XF2 code bad had been rewritten from scratch. You'll find stuff in similar places, but as some concepts and all the code changed, you shouldn't use or refer to XF1 code snippets anymore for your implementation. You still want to use the entity system to edit the user and trigger the cache rebuild via it.
 
So, I need to use an entity to insert a row into the user field values table.
What I can see from the documentation doesn't seem to identify how to actually insert data using an entity. Lots about defining the structure, but this table already exists.

What I'm able to glean leads me to starting here:
PHP:
$structure->table = 'xf_user_field_value';
$structure->shortName = 'XF:UserFieldValue';
$structure->primaryKey = ['user_id','field_id'];

But then I genuinely have no idea. I feel like this should be simpler than I'm making it... it's just inserting a row for a user that's called by this external software. The documentation seems to assume a lot of knowledge about Xenforo (and familiarity with previous iterations of XF) than I have.
 
Last edited:
$entity->field = 'data';
$entity->save();

In your case it would be

$user->secondary_group_ids = [1,2,3];
$user->save();
 
Hey @S Thomas - thanks, I remember you helped me in a previous development conundrum.

Based on what you said, and needing to insert a new row in the user_field_value table, this is what I've got to help create the bridge so far:
PHP:
<?php   

$userID = $_GET['id'];
$tokenValue = $_GET['token'];

const XF_ROOT = __dir__; // change this as needed
require(XF_ROOT . '/src/XF.php');
\XF::start(XF_ROOT);
$XF = \XF::setupApp('XF\Pub\App');
$XF->start();



$structure->table = 'xf_user_field_value';
$structure->shortName = 'XF:UserFieldValue';
$structure->primaryKey = ['user_id','field_id'];

$UserFieldValue->user_id = "$userID";
$UserFieldValue->field_id = "73797374656d5f70617373";
$UserFieldValue->field_value = "$tokenValue";
$UserFieldValue->save();

?>
This is creating the error:
[E_WARNING] Creating default object from empty value
updategroup.php:14

Line 14 is something that comes almost directly from the documentation, though - so I'm not sure what I've done wrong. Perhaps I don't need those structure statements?
 
Is anybody able to tell what I'm doing wrong here?
I'd hoped inserting this row would just be a one line kind of deal, but the cache has definitely made it more of an ordeal than expected!
 
Top Bottom