We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40021
    • 4 Posts
    Hello,
    I can`t get an alias after creating a new resource in a snippet. This only happens when I request the alias in the snippet I created the rescource.
    Here is an example:
    //Create Resource
    $fields['createdby'] = 1;
    $fields['parent'] = 10;
    $fields['template'] = 2;
    $fields['alias'] = date("Y-m-d_H:i:s");
    $fields['hidemenu'] = 1;
    $fields['published'] = 1;
    $fields['pagetitle'] = date("Y-m-d_H:i:s");
    
    $response = $modx->runProcessor('resource/create', $fields);  
    
    if ($response->isError()) {
    	// An error occurred
    	if ($response->hasFieldErrors()) {
    	  $fieldErrors = $response->getAllErrors();
    	  $errorMessage = implode("\n", $fieldErrors);
    	}
      else {
        $errorMessage = 'An error occurred: ' . $response->getMessage();
      }
      $modx->log(xPDO::LOG_LEVEL_ERROR,$errorMessage);
    }
    
    //request resource for Processor
    $sgMessage = $response->getObject();	//contains only ID
    $sgMessageId=$sgMessage['id'];
    $sgMessage=$modx->getObject('modResource', array('id'=>$sgMessageId));	//get rescource object
    //set content
    $sgContent="test";
    $sgMessage->set('content',$sgContent);
    $sgMessage->save();
    
    //FAILS: The Url isn`t in the aliasMap
    $sgURL=array_search($sgMessageId,$modx->aliasMap);
    print_r($modx->aliasMap); //Debug aliasMap
    
    //FAILS: depent on aliasMap
    $sgMessageLink=$modx->makeUrl($sgMessageId, '', '', 'full');
    
    //WORKS for every other ID except the created:
    $sgMessageLinkFix=$modx->makeUrl(125, '', '', 'full');
    
    

    Modx Revo 2.2.2.

    Thanks in advance to anyone who can help

    This question has been answered by multiple community members. See the first response.

      • 22303 MODX Staff
      • 10,725 Posts
      You have to refresh the cache (the alias map is cached into the context_settings cache partition) and reload the context data before the new alias is available.
        • 4172
        • 5,888 Posts
        you will need to run modCacheManager->generateContext($context_key) before you can use makeUrl for new created resources
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 40021
          • 4 Posts
          $modx->cacheManager->refresh();
          //OR / AND
          $modx->cacheManager->generateContext('web');
          


          The same result -> no correct AliasMap [ed. note: sascha007 last edited this post 14 years, 3 months ago.]
          • discuss.answer
            • 4172
            • 5,888 Posts
            did you try
            $fields['syncsite'] = true;  


            before
            $response = $modx->runProcessor('resource/create', $fields);  


            and then
            $modx->cacheManager->generateContext('web');
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 40021
              • 4 Posts
              ok I tried it. No success
              • discuss.answer
                • 22303 MODX Staff
                • 10,725 Posts
                Instead of modCacheManager::generateContext() after calling the processor with the syncsite option, you need to use modX::reloadContext(), e.g.
                $modx->reloadContext('web');
                
                  • 40021
                  • 4 Posts
                  Thank you. Now it works perfectly.