1
$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.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
YanAlex
  • 99
  • 1
  • 10
  • Why are you setting the TV value and then setting it back again? Perhaps what you are trying to do can be achieved differently, for example storing your first value in a variable for use on the current page, as well as changing the TV value so it's stored permanently after the page is refreshed. – okyanet Dec 27 '12 at 15:01

2 Answers2

0

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));
Vasis
  • 2,281
  • 1
  • 16
  • 23
  • Ive tried do it ...the following code outputs - 2 and 2 $modx->resource->setTVValue(11, 1); $modx->resource->save(); print_r($modx -> resource -> getTVValue(11)); $modx->resource->setTVValue(11, 2); $modx->resource->save(); print_r($modx -> resource -> getTVValue(11)); – YanAlex Dec 27 '12 at 10:00
  • I want to say, the object is really changes and in manager i can see the changed TV value, but the code returns the old value and only after refreshing the page i see the new value, donno why ((( – YanAlex Dec 27 '12 at 10:04
  • Following code doesnt work too ... ((( cry cry `$tv = $modx->getObject('modTemplateVar',array('id'=>'11')); $tv -> setValue($modx->resource->get('id'), 150); $tv->save(); $res = $modx->getObject('modResource',array('id'=>$modx->resource->get('id'))); print_r($res -> getTVValue(11));` – YanAlex Dec 27 '12 at 10:09
  • `$id_resource = $modx->resource->get('id'); $id_tv = 11; $value = 8228; $tv = $modx->getObject('modTemplateVar',array('id'=>$id_tv)); $tv -> setValue($id_resource, $value); $tv->save(); $res = $modx->getObject('modResource',array('id'=>$id_resource)); print_r($res->getTVValue($id_tv));` You wouldnt believe but the following still shows change only after refreshing the page. – YanAlex Dec 27 '12 at 10:15
  • You do uncacheable snippet call? As example - `[[!my_code_snippet]]` – Vasis Dec 27 '12 at 12:16
0

You are going to love this:

$changeit = 11;
//$changeit= 'templateVarName';
$r = $modx->getObject('modResource', 177);
// or $r = $modx->resource
$tvs = $r->getTemplateVars();
if ($tvs) {
    foreach ($tvs as $object){
        if (is_object($object) && $object instanceof modTemplateVar){
           print_r($object->toArray());
        }
    }
 }

$r->setTVValue($changeit,'99999fhfhg');
echo '<hr>'. $r->getTVValue($changeit);
$r->setTVValue($changeit,'888885454564');
echo '<hr>'. $r->getTVValue($changeit);
$r->setTVValue($changeit,'123456789');
echo '<hr>'. $r->getTVValue($changeit);

By sending it

$changeit = '11';

It becomes a string and is_string is true and thereby processes the request as if the name is 11, not the ID. You can send either the id or the name of the TV. I personally use only names, as it is much easier to deal with later.

The code I placed works and was tested.

I will be filing a bug report on this, because it should be a tad cleaner.

Also, you retrieve the current resource, update it etc. But you do not reload a "fresh" version of the resource after it is updated. You are in affect looking at the same var at the same state twice.

The mod->resource is the same as when you entered the page.

By making it a var it gets updated as you process through your changes.