How to correct create new Document from Snippet?

I have already written a snippet and can successfully create a new document. But unfortunately I have the problem after saving that the alias of the doc is empty in the cache. In particular I mean, the generated link to this doc is without the alias eg. www.some-domain.org/parentdoc/ and here should be the alias of the doc, but isn’t So as a result the page is not callable via the friendly URL. But when I take a look on the alias in the manger it’s there. It’s just a ,matter of caching I guess. As soon as I delete the cache everything works fine and the document is accessible via the alias (friendly URL).

However, there is one thing to mention in my code. Using Formit (in the frontend) a form is submitted and based on this the snippet creates a document. To avoid duplicate aliases, I use a generated uniqid.

So far so good, here is my code:

$doc = $modx->newObject('modDocument');
$doc->set('parent', $ParentID);
$doc->set('pagetitle', $Pagetitle);
//generate unique id for page alias
$prefix = 'inserat-';
$Alias = uniqid($prefix);
//example of Alias: inserat-60154384e5de7
$doc->set('alias', $Alias);
$doc->set('template', $TemplateID);
$doc->set('published', $Published);
$doc->set('hidemenu', $HideMenu);
$doc->set('cacheable', $Cacheable);
$doc->setContent($Content );
$doc->save();
$docID = $doc->get('id');
// after that line some TV gets filled in with 
$tv->setValue($docID, 'some text fot the tv');  
$tv->save();

Actually I would be super happy with this snippet, except for the problem with the cache. As a quick fix, I clear the entire cache every time with the snippet (CacheClear) from Bobray. I just use it as a hook in the formit call.

Am I doing something wrong when creating the doc in the snippet? If not, can I remove only this
document once from the cache? Always clearing the entire cache is not a good solution in the long run.

Thanks to all!

You can clear the cache in the snippet yourself (without calling the CacheClear snippet in a hook).

$modx->cacheManager->refresh();

You can also provide parameters which Cache Partitions that you want to be cleared. Maybe it is enough just to clear “context_settings” that contains an alias map.

1 Like

HI halftrainedharry,

that’s fine and saves execution time. Thanks!

But it will, no matter which partition I choose, always affect all documents. It would be nice to cache exactly only the doc in question again so that the alias is also correctly in the cache.

partial caching is challenging
maybe you can create your own solution. Have look into modCacheManager->generateContext

Thanks @bruno17, will take a closer look into that.

In the meantime I found this article written by Bobray some years ago: https://blog.arvixe.com/clearing-the-cache-for-selected-resources-in-modx-revolution/
Is this a way worth to follow for me? I’m just asking because the article is from 2014. I would otherwise never question Bobray’s tips :wink:

I think, you would still have to clear and recreate the aliasMap or disable the cache_alias_map

Thanks @halftrainedharry and @bruno17!
Tested Bobray’s snippet without success and did not check in detail why.
Instead, I now use this:

$modx->cacheManager->refresh([
    'context_settings' => ['contexts' => ['web']]
]);

And I think that does a great job. I’m totaly fine this way…

Thanks again!

1 Like

I think $modx->reloadContext('context name'); might do that.

This is what I have, though I haven’t tested it in a very long time:

$res = $modx->getObject('modResource',array('id'=>5));
$modx->cacheManager->delete($res->getCacheKey(), array(
            xPDO::OPT_CACHE_KEY => $modx->getOption('cache_resource_key', null, 'resource'),
            xPDO::OPT_CACHE_HANDLER => $modx->getOption('cache_resource_handler', null, $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
            xPDO::OPT_CACHE_FORMAT => (integer) $modx->getOption('cache_resource_format', null, $modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)))
);

(You have to insert the correct resource ID.)