Hi there,
I think I’ve found a bug, but maybe someone can spot something I am doing wrong..
Trying to generate a link from a brand newly created modResource document:
<?php
$criteria = array(
'alias' => $newalias,
'parent' => $folder,
'context_key' => 'act'
);
$document = $modx->getObject('modResource',$criteria);
if(!$document){
// document does not exist
// document generation required.
$content = $modx->runSnippet('generateReport');
// create report page by adding to criteria
$criteria['content'] = $content;
$criteria['published'] = 1;
$criteria['template'] = 10;
$criteria['menutitle'] = $readableDate;
$pagetitle = 'Report page';
$criteria['pagetitle'] = $pagetitle;
$criteria['longtitle'] = $pagetitle;
$criteria['richtext'] = 0;
$criteria['publishedon'] = $datePoint;
$criteria['createdby'] = 1;
$newpage = $modx->newObject('modResource',$criteria);
if($newpage->save() !== false){
// this line was found at http://modxcms.com/forums/index.php?topic=46794.0
$modx->cacheManager->clearCache();
// hoped it would solve the problem but no!
$id = $newpage->get('id');
// also tried calling the object all over again here, ie.
// $doc = $modx->getObject('modResource',$criteria);
// $id = $doc->get('id');
// but no joy..
$content .= '<p>Page generated successfully: <a href="' . $modx->makeUrl($id) . '">'.$newpage->get('longtitle').'</a></p>';
// LINK DOESNT WORK HERE, BUT IF I ECHO OUT $id ITS CORRECT! AND $newpage->get('longtitle') IS ECHOD OUT FINE TOO!
}else{
$content .= ' <p>Page generation failed</p>';
}
}else{
// document already exists
// may as well use content from existing page
$criteria = $document->toArray();
$id = $criteria['id'];
$content = $criteria['content'] . '<p>From existing page <a href="' . $modx->makeUrl($id) . '">'.$criteria['longtitle'].'</a></p>';
// THIS LINK WORKS..
}
return $content;
?>
So when the script first loads and the page is generated the link is broken,
yet on subsequent loads after the resource is generated the link works!
It’s really important the link works on first load as I want to send it as an email,
can anybody see what I am doing wrong here?
Many thanks,
Toby