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.