We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    That sounds really frustrating and it sure looks like a bug. My guess is that the resource is saved, but the TV related object is not unless the resource cache is empty.

    I think getting the templateVarResource object directly would be the fastest workaround. It would be faster if you used the ID of the TV rather than the name. It should be much faster than what you were doing, since there's no need to save the resource:

    <?php
    
    $fields = array(
        $tvId = 12;  // ID of the TV
        'contentid' = $modx->resource->get('id'),
    );
    
    $tvr = $modx->getObject('modTemplateVarResource', array('tmplvarid'=>$tvId,  'contentid' = $modx->resource->get('id')));
    
    $count = $tvr->get('value');
    $count++;
    $tvr->set('value', $count);
    $tvr->save();
    
    return $count;



    Please report the bug so it will get fixed.


    ---------------------------------------------------------------------------------------------------------------
    PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
    MODx info for everyone: http://bobsguides.com/modx.html
      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
      • 31037
      • 358 Posts
      Thank you very much!

      When reporting the bug I found a very old issue in the tracker, still open:

      http://tracker.modx.com/issues/5294

      I guess that is same bug? Or should I enter a new one?

      Too bad I didn't find it earlier. tongue


      Thanks for the new faster way of doing the counter, works fine. I will use that way of working with tvs on other places too.

      So, if someone would want it, here is a working code for a simple page hits counter, with a simple realod prevention:

      $ip = $_SERVER['REMOTE_ADDR'];
      $tvPageHitsId = 2;
      $tvLatestIpId = 4;
      $resId = $modx->resource->get('id');
      
      $tvPh = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvPageHitsId, 'contentid' => $resId));
      $tvLip = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvLatestIpId, 'contentid' => $resId));
      
      if ($tvPh) {
          $count = $tvPh->get('value');
          if (!$count) { $count = 0; }
      } else {
          $tvPh = $modx->newObject('modTemplateVarResource', array('tmplvarid' => $tvPageHitsId, 'contentid' => $resId));
      }
      
      if ($tvLip) {
          $latestIp = $tvLip->get('value');
      } else {
          $tvLip = $modx->newObject('modTemplateVarResource', array('tmplvarid' => $tvLatestIpId, 'contentid' => $resId));
      }
      
      if ($ip != $latestIp) {
          $count++;
          $tvPh->set('value', $count);
          $tvPh->save();
      }
      
      $tvLip->set('value', $ip);
      $tvLip->save();
      
      return $count;
      [ed. note: Uncle68 last edited this post 14 years, 7 months ago.]
        • 10378
        • 375 Posts
        Hi Uncle68 and @BobRay

        thanks a lot for posting this workaround/solution! I'm having the same problem and searched for a solution for about 3 weeks!

        I also filed a bug - hopefully this gets fixed soon!

        Greetings,
        Martin
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 3749
          • 24,544 Posts
          [Update] It's six months later and I just ran into this bug again in 2.2.6 on Cloud. Found this thread when looking for a solution. LOL. wink

            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