We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38136
    • 42 Posts
    Hi,

    i wrote a bit of code. which was working before i implemented some caching:

    /* caching */
    $output = '';
    $cacheOptions = array(xPDO::OPT_CACHE_KEY => 'mycache');
    $cacheKey = 'path/'.md5($_SERVER['REQUEST_URI']);
    $cacheManager = $modx->getCacheManager();
    $cacheLifeTime = strtotime("tomorrow 00:00:00") - time();
    
    if(is_null($output = $cacheManager->get($cacheKey, $cacheOptions))) {
          /* ... */
    
          $obj = $modx->getObject('modResource', $modx->resource->get('id'));
          $TVs = array();
          foreach ($obj->getMany('TemplateVars') as $tv) {
            $TVs[$tv->get('name')] = $tv->getValue($modx->resource->get('id'));
          }
          /* somehow $TVs is always array(0) */
    
          /* ... */
    
          $cacheManager->set($cacheKey, $output, $cacheLifeTime, $cacheOptions);
    }
    
    return $output;
    


    well, also if i disable caching in that code $TVs still is empty. actually i didnt realy touched the getMany() statements in the last days. it was runing before i implemented the caching.

    is there something special to consider about caching? is getMany() somehow influenced by caching?
      • 3749
      • 24,544 Posts
      Try replacing this:

      $obj = $modx->getObject('modResource', $modx->resource->get('id'));
            $TVs = array();
            foreach ($obj->getMany('TemplateVars') as $tv) {
              $TVs[$tv->get('name')] = $tv->getValue($modx->resource->get('id'));
            }



      with this:

      $tvs = $modx->resource->getMany('TemplateVars');
      if (count($tvs) == 0) {
         return 'No TVs retrieved');
      }
      $myTVs = array();
      foreach ($tvs as $tv) {
          $myTVs[$tv->get('name')] = $tv->getValue($modx->resource->get('id'));
      }
      
      return print_r($myTVs, true);
      
      


      (You may have to go back to getting the resource object first.)
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38136
        • 42 Posts
        Quote from: BobRay at Jun 12, 2016, 05:25 PM
        Try replacing this:

        $tvs = $modx->resource->getMany('TemplateVars');
        if (count($tvs) == 0) {
           return 'No TVs retrieved';
        }
        $myTVs = array();
        foreach ($tvs as $tv) {
            $myTVs[$tv->get('name')] = $tv->getValue($modx->resource->get('id'));
        }
        
        return print_r($myTVs, true);
        
        


        (You may have to go back to getting the resource object first.)

        Thank you for your post. i am getting "No TVs retrived" if i copy and paste your code in my snippet. actually i am using the getMany in a foreach where i look through some childs and i am grabbing the childs TVs.

        But i figured out that getMany always is empty. Also when i use it in other snippets.

        I am considering to rewind the backup copy form the day before yesterday where it was runing. Then i might use "diff" to figure out what changed. But thats a lot of work sad