2

I have the following snippet code hooked up to a FormIt email form:

$tv = "taken" . (int)$hook->getValue('datetime');
$docID = $modx->resource->get('id'); //get the page id
$page = $modx->getObject('modResource', $docID);
$current = (int)$page->getTVValue($tv);
if (!$page->setTVValue($tv, $current + 1)) {
    $modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving your TV...');
}
$modx->setPlaceholder('successMessage','<h2 class="success">'.$current.'</h2>');
return true;`

It increments a template variable every time it is run and outputs a success message (although right now I'm using that functionality to output a debug message instead). The problem is, it only increments the TV once after saving the snippet, thereby refreshing the cache. Normally I would call the snippet without cache by appending ! to its name, but that doesn't appear to work for FormIt hooks. How can I get this code to work? Right now I'm running the entire page as uncacheable, but that is obviously suboptimal. Perhaps, there's a way to hook a snippet in an uncached manner? Call a snippet from within a snippet as uncached?

curveball
  • 4,320
  • 15
  • 39
  • 49
kotekzot
  • 1,518
  • 1
  • 14
  • 23

2 Answers2

0

I'm doing something similar - but to count page loads, it looks to me like you are missing the last little bit: $current->save();

<?php
$docID = $modx->resource->get('id');

$tvIdm = 32;
$tvm = $modx->getObject('modTemplateVar',$tvIdm );
$tvm->setValue($docID, $tvm->getValue($docID) + 1 );
$tvm->save();
Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
  • Unfortunately this fails in the same way - works on 1st run, then stops; resaving gives me 1 more good run, then it stops working again. Well, it would be a miracle if it didn't - setTVvalue commits automatically and if it didn't it wouldn't increment on the first run either. – kotekzot Dec 06 '12 at 09:46
0

Try add this before you save $tv object

$tv->_processed = false;

It's derived from modElement's property it extends.

goldsky
  • 801
  • 7
  • 11