Hello,
reading the thread title - the first thing that comes into mind is - this guy hasn't tuned off caching for his snippets! Unfortunately this isn't the case - I'm calling both snippets uncached.
I try to implement a simple click counter in
MODx Revo and therefore I made two simple snippets:
Snippet
ClickCounterSet which increases and writes the TV value of the TV
clickCounter
<?php
$tv = $modx->getObject('modTemplateVar',array('name'=>'clickCounter'));
if (!$tv) {
$modx->log(xPDO::LOG_LEVEL_ERROR, 'TV clickCounter not set');
return;
}
$id = $modx->resource->get('id');
$page = $modx->getObject('modResource', $id);
$newVal = $page->getTVValue('clickCounter') + 1;
if (!$page->setTVValue('clickCounter', $newVal)) {
$modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not save TV clickCounter');
}
return;
and Snippet
ClickCounterGet which reads and returns the TV value of the TV
clickCounter
<?php
$tv = $modx->getObject('modTemplateVar',array('name'=>'clickCounter'));
if (!$tv) return '(TV clickCounter not set)';
$id = isset($resourceid) ? $resourceid : $modx->resource->get('id');
$page = $modx->getObject('modResource', $id);
$clicks = $page->getTVValue('clickCounter');
return ($clicks > 0) ? $clicks : 0;
Both snippets are called uncached but the value is only increased once (when the resource is loaded the first time after cache is cleared). Reloading the resource doesn't write the increased value to the TV. Clearing the cache causes the write process to work once again - and so on ...
What am I doing wrong? This thing slowly drives me nuts...
By the way: no message is written to the MODx system log!
Using MODx Revo 2.2.4-pl
[ed. note: gadgetto last edited this post 14 years, 3 months ago.]