Search code examples
modxmodx-revolutionmodx-resources

MODx Revo setTvValue returns old data


$modx -> resource -> setTVValue(11, 1);
print_r($modx -> resource -> getTVValue(11));

$modx -> resource -> setTVValue(11, 2);
print_r($modx -> resource -> getTVValue(11));

I have a snippet that outputs 2 and 2, whereas it should output 1 and 2. Resource caching is turned off; snippet call is not cached either.

I have tried to fix this problem by another way, but it still updates my TV only after the whole page gets reloaded:

$tv = $modx->getObject('modTemplateVar',array('id'=>'11'));
$tv -> setValue($modx->resource->get('id'), 88);
$tv->save();

print_r($modx -> resource -> getTVValue(11));

By the way, if I am not working with TVs, everything is fine!

$modx -> resource -> set("pagetitle", 1);
print_r($modx -> resource -> get("pagetitle"));

$modx -> resource -> set("pagetitle", 2);
print_r($modx -> resource -> get("pagetitle"));

How do I fix this issue with TVs? I've tried to clear cache like this $modx->cacheManager->refresh(); too, but it didn't do the trick.


Solution

  • Ok try this

    $id_resource = $modx->resource->get('id');
    $id_tv = 11;
    $value = 88;
    
    $tv = $modx->getObject('modTemplateVar',array('id'=>$id_tv));
    $tv -> setValue($id_resource, $value);
    $tv->save();
    

    if you need to get resource try this

    $id_resource = $modx->resource->get('id');
    $id_tv = 11;
    
    $res = $modx->getObject('modResource',array('id'=>$id_resource));
    print_r($res->getTVValue($id_tv));