We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31037
    • 358 Posts
    This code was working fine in 2.08:
          $count = $tv->getValue($id);
          $count++;
          $tv->setValue($pageId,$count);
          $tv->save();

    But when upgrading to 2.2pl2 the value only updates one time after clearing the cache, and then no more.

    I've tried running it as plugin and snippet, cached and no cached... What am I missing?
      • 31037
      • 358 Posts
      No one can give me a hint on what has changed?
        • 33968
        • 863 Posts
        Can you post your full code from that snippet? Also your snippet call?
          • 31037
          • 358 Posts
          The only code before this is code for getting the resource id. Everything is working fine, it just wount update more than first load efter empty cache.

          I've tried run it as both snippet, cachen and uncached, and also as plugin.

          Thought it would be easy for someone knowing modx to know what has changed that make this code not working anymore.

          Guess I'll make a new counter, seems like waist of time though, if there were an easy answer to this.

          Thanks
            • 3749
            • 24,544 Posts
            We really can't help you without seeing more of the code. For example, what code are you using to get the TV object and what does the snippet return. Does the snippet return the value to be displayed (it would be more efficient if it did) or are you using a TV tag for that or are you setting a placeholder?

            Also, did you try making the tag that displays the value on the page uncached?


            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODx info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 7 months ago.]
              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
              Sorry, I thought I had shown the relevant parts of the code, but I see now I left out how I get the object. Pretty important part.

              The complete code is as follows:
              $id = $modx->resource->id;
              $count = 0; // Needs to be a number if new page
              $tv = $modx->getObject('modTemplateVar', array('name'=>'docHits'));
              $count = $tv->getValue($id);
              $count++;
              $tv->setValue($id,$count);
              $tv->save();
              return;


              I run it from the content of the page like [ [!MyPageCounter]]. Same things happens if I run as plugin but to make it easy i here run it as a snippet.

              In the example below I show the TV value as in database, to be sure it isn't a display problem. (On the page I show the tv value, as I usually run this as a plugin. But when it works I'll change to a snippet instead as BobRay suggested it will be faster. Again, the values here are from database, this is not a display error.)

              Let´s say my counter is at 100.

              This is what happens if the page from where it is called is CACHED:

              I empty the cache manually.
              Reload the page
              The TV value (checked in database) is now 101, perfect.
              I reload the page. The tvvalue in db is still 101.
              Reloads 5 times. Value still 101.
              Clears cache, reloads page. Value now 102! Nice.
              Reloads 5 times. Value still 102.
              Clears cache. Reloads. Value 103.
              And so on.

              Ok, let's make the PAGE from where the snippet is called UNcached:
              Reload. Value 104. Reload: 105.
              Reloads 5 times: 106,107,108, 109, 110.
              Working. But I can't have the page uncached.

              Perhaps I should try using getTVvalue instead? But then I have to first get the resource object, seems not that efficient?

              This code worked fine in 2.08.

              I wonder what I'm doing wrong, the time I've spent on this little problem is the same as I use making two complete sites. sad
                • 33968
                • 863 Posts

                Perhaps I should try using getTVvalue instead? But then I have to first get the resource object, seems not that efficient?
                You already have access to the current resource object by default, so no worries there. You can simply do this:
                $count = $modx->resource->getTVValue('tvName');
                $modx->resource->setTVValue('tvName', $count + 1);
                

                For some reason your snippet is getting cached at element level. I wonder if you've uncovered a bug...

                Try doing a little test and see if this works. Comment out the other code in your snippet and leave:
                return time();
                

                Does the output increase every time you load the page, or does it remain cached at the first value?
                [ed. note: okyanet last edited this post 14 years, 7 months ago.]
                  • 31037
                  • 358 Posts
                  Lucas, thank you very much for trying to help me. I could of course make another different counter that save the counter to some other table, but I want to know if I'm doing something wrong, best way of learning.

                  Ok, now I have this code:
                  $id = $modx->resource->id;
                  $count = $modx->resource->getTVValue('docHits');
                  $modx->resource->setTVValue('docHits', $count + 1);
                  $modx->resource->save;
                    
                  return 'New count = ' . $count . '
                  Time = ' . time();


                  The new way of getting/setting tv value didn't change anything, everything behaves exactly as before.

                  The time value updates when the snippet is called unchaded, but counter doesn't count up. (More than the first time after clearing the cache, just like before.

                  I log other stuff with snippets and plugins, but then to separate tables. All that works fine.

                  Not a snippet problem since everything behaves the same if running it as plugin. And code worked fine before upgrade 2.08 -> 2.02.

                  Problem is only when using TVs. TVs seem to cache themself even the snippet with the code is called uncached.


                  I never use the word *bug* until some else does it first. But on the other hand I never asks questions until I believe it is a bug. tongue [ed. note: Uncle68 last edited this post 14 years, 7 months ago.]
                    • 3749
                    • 24,544 Posts
                    If you reload the page 5 times, clear the cache, then reload the page, does the count go up by 5? That will show whether the problem is in updating the TV or updating the display.

                    Is the tag to display the count on the page uncached?

                    [[!*count]]


                    ---------------------------------------------------------------------------------------------------------------
                    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
                      It is not a display error, I check the value in db or in manager, the problem is the tv value not getting updated.

                      If I reload 5 times, the counter go up with ONE (if I emptied the cache before the uploads).

                      ONLY the first page load *directly* after clearing cache updates the tv value.

                      If I have the resource uncached the counter updates every time. My Reply #6 explains this with longer example.

                      [ed. note: Uncle68 last edited this post 14 years, 7 months ago.]