Answer the question
In order to leave comments, you need to log in
Why does assigning a value to TV work every other time?
There is a plugin that fires during the 'OnWebPagePrerender' event, inside the plugin I perform a series of actions and save the resource's TV value like this:
$tvcid = $modx->getObject('modTemplateVar',array('name' => 'views'));
$tvcid->setValue($modx->resource->get("id"), $views);
$tvcid->save();
$modx->cacheManager->clearCache();
$modx->resource->setTVValue('views', $views);
$modx->resource->save();
$views = $modx->resource->getTVValue("views");
$views = empty($views) ? 1 : ((int)$views + 1);
Answer the question
In order to leave comments, you need to log in
As a result, the problem was in the cache of the resource itself. Despite the fact that the plugin executed correctly every time, regardless of the cache, the resource itself was cacheable and, as a result, gave out-of-date data. In my case, disabling the resource cache is completely inappropriate, I solved the problem by creating a new table in the database.
$views = $modx->query("SELECT * FROM `news_views` WHERE `news_id`='{$modx->resource->get('id')}'");
$views = $views->fetch(PDO::FETCH_ASSOC);
if(empty($views)){
$modx->query("INSERT INTO `news_views`(`news_id`, `count`) VALUES({$modx->resource->get('id')}, 1)");
}else{
$views = $views["count"] + 1;
$modx->query("UPDATE `news_views` SET count=count+1 WHERE news_id={$modx->resource->get('id')}");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question