We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 895
    • 36 Posts
    What am I doing wrong huh The file with the cache is not recorded. The screen displays a large array.

    <?php
     
    $cache = 'blocks/blog/last10posts';
    $cachedResult = $modx->cacheManager->get($cache);
    
    $result = array();
    
    	if (!empty($cachedResult))
    
    		$result = $cachedResult;
    		
    	else {
    
    		$childs = $modx->getChildIds(2, 1);
    
    		$c = $modx->newQuery('modResource');
    		$c->innerJoin('modTemplateVarResource', 'TemplateVarResources');
    		$c->innerJoin('modTemplateVar', 'TemplateVar', '`TemplateVar`.`id` = `TemplateVarResources`.`tmplvarid`');
    		$c->select('modResource.publishedon, modResource.pagetitle, modResource.createdby, modResource.id');
    		$c->where(array(
    		   'context_key' => 'web',
    		   'isfolder'=> false,
    		   'parent:IN' => $childs,
    		   'deleted' => false,
    		   'hidemenu' => false,
    		   'published' => true,
    		));
    		$c->where(array(
    			'TemplateVar.name' => 'Tags',
    			'TemplateVarResources.value:LIKE' => '%modx%',
    		));
    		$c->sortby('menuindex','ASC');
    		$c->limit(10);
    
    
    		/* 
    		$c->prepare();
    		echo "RAW SQL : ".$c->toSQL();
    		*/
    
    		$result = $modx->getCollection('modResource', $c);
    		
    		$modx->cacheManager->writeTree($cache);
    		$modx->cacheManager->set($cache, $result, 3600);
    	}
    
    foreach($result as $resource){
    	$output .= $modx->getChunk('blog', array(
    					'publishedon' => $resource->get('publishedon'),
    					'pagetitle' => $resource->get('pagetitle'),
    					'createdby'=> $resource->get('createdby'),
    					'id'=> $resource->get('id'),
    				)
    		);
    }
    
    return $output;
    ?>
    



    Whis works, but writes a sting to the cache.
    <?php
     
    $cache = 'blocks/blog/last10posts';
    $cachedResult = $modx->cacheManager->get($cache);
    
    $result = array();
    
      if (!empty($cachedResult)){
    
        $output = $cachedResult;
        
      } else {
    
        $childs = $modx->getChildIds(2, 1);
    
        $c = $modx->newQuery('modResource');
        //$c->innerJoin('modTemplateVarResource', 'TemplateVarResources');
        //$c->innerJoin('modTemplateVar', 'TemplateVar', '`TemplateVar`.`id` = `TemplateVarResources`.`tmplvarid`');
        $c->select('modResource.publishedon, modResource.pagetitle, modResource.createdby, modResource.id');
        $c->where(array(
           'context_key' => 'web',
           'isfolder'=> false,
           'parent:IN' => $childs,
           'deleted' => false,
           'hidemenu' => false,
           'published' => true,
        ));
        /*$c->where(array(
          'TemplateVar.name' => 'Tags',
          'TemplateVarResources.value:LIKE' => '%modx%',
        ));*/
        $c->sortby('menuindex','ASC');
        $c->limit(10);
    
    
        
        //$c->prepare();
        //echo "RAW SQL : ".$c->toSQL();
        
    
        $result = $modx->getCollection('modResource', $c);
        
        foreach($result as $resource){
          $output .= $modx->getChunk('blog', array(
                            'publishedon' => $resource->get('publishedon'),
                            'pagetitle' => $resource->get('pagetitle'),
                            'createdby'=> $resource->get('createdby'),
                            'id'=> $resource->get('id'),
                          )
                );
        }
        $modx->cacheManager->set($cache, $output, 3600);
      }
    
    return $output;
    ?>