After resource install 'bad link tag' due to not being in aliasMap?

Summary

I’m getting an error although the page in question (with id = 170) works fine.
[2025-01-15 23:42:37] (ERROR in resource 170 @ …/core/src/Revolution/modLinkTag.php : 108) Bad link tag [[~170?&scheme=full]] encountered

Environment

MODX version 3.0.6, MySQL version 15.1, browser Firefox/Brave.

Step to reproduce

I have PHP code that installs my resources. I copy all the static files into the right places then use modx->newObject(‘MODX\Revolution\modStaticResource’) to create resources and $obj->set() to set the properties, finally $obj->save().

The error is shown because, in modLinkTag.php, $this->_output is empty after setting it with $this->modx->makeUrl(…).
Now modContext::makeUrl() calls modContext::getResourceURI() where the URI is empty after $uri = array_search($id, $this->aliasMap).
The aliasMap is beyond my understanding.

When I go into the dashboard and Save the resource manually the error goes away.
The row in the modx_site_content table is not altered when I save except for ‘editedon’ was filled and ‘introtext’ went from NULL to empty string.
In fact the modx_site_content table row looks the same if I manually add a resource as it does when I use my script to add it.
My script sets:
‘alias’ => ‘privacy’,
‘cacheable’ => 0,
‘content’ => core/components/privacy.html,
‘description’ => ‘Privacy’,
‘hidemenu’ => 1,
‘pagetitle’ => ‘Privacy’,
‘parent’ => 76,
‘searchable’ => 0,
‘template’ => 8,
‘uri’ => ‘privacy’,
‘uri_override’ => 1,
also
‘createdby’ => 1
‘published’ => 1
‘publishedby’ => 1

Leaving these as their defaults … alias_visible = 1, introtext = NULL, and isfolder = 0. In the DB the class_key is ‘MODX\Revolution\modStaticResource’.

I have a few dozen resources but I really don’t want to have to click on Save for each after installing them with my script.
So what am I doing wrong?

My guess is that you just have to clear the cache (e.g. $modx->cacheManager->refresh();) after adding the new resources by code.

This should then recreate the aliasMap in the cache file of the context (so that it contains the newly added resources as well).

Brilliant! That did it, I should have thought of that. Many thanks.