Pregunta

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

¿Fue útil?

Solución

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));

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top