We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I might be asking the "obvious" but can you use setTVValue() on a modResource object BEFORE you have saved that object? I've been trying to create a list of resources programmatically -- I haven't had any problems with creating the main pages, but so far, none of my TV values get created.

    What caveats are there for using this function? E.g. what if the named TV doesn't exist? Does the function fail? What happens if you try to run it before you save the modResource object?

    E.g.

    $page = $modx->newObject('modResource');
    $page->set('pagetitle', 'Sample...');
    // ... other page attributes...
    $page->setTVValue('my_tv', 'some value');  // Ex. 1: does nothing ?
    $page->save();
    $page->setTVValue('my_other_tv', 'other value');  // Ex. 2: also does nothing ?
    


    Can anyone point me in the right direction? I could use the modTemplateVarResource to do this verbosely, but I wanted to clarify the limitations of the setTVValue function since it's not documented too well...
      • 3749
      • 24,544 Posts
      First, you probably know this, but just in case -- if a Resource's TV value is the same as the TV's default value, *nothing* is saved to the DB and it can make you think that your code is failing. This confused me a lot when I first started working with TVs in code.

      Earlier versions of Revo had a bug that wiped all TV values if you called the processor -- I'm not sure it that's fixed or not.

      I think if you save a TV value before the doc is saved by a MODX form (e.g., in OnBeforeDocFormSave), the value you set will be overwritten. If, however, you do it before saving the Resource yourself with $resource->save(), it should work. It should also work if you're using the Resource/Create or Resource Update processor, assuming that the bug is fixed.

      All that said, why not do it after the resource is saved? It won't take any more time (since it's a different table) and it's pretty much guaranteed to work.

      I should also mention that it's possible to include the TV values in a call to $modx->runProcessor(), though it's a little tricky.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • $tv = $modx->getObject('modTemplateVarResource', array('tmplvarid' => xx, 'contentid' => $page->id));
        if(empty($tv)){$tv = $modx->newObject('modTemplateVarResource', array('tmplvarid' => xx, 'contentid' => $page->id));}
        $tv->set('value', 'some value');
        $tv->save();
        
          Mat Dave Jones
          • 3749
          • 24,544 Posts
          You can also use this, which might be a few milliseconds faster, since getObject() returns null on failure:

          if (!$tvr) {
          }


          I would use $tvr for the variable name since you're not really getting the TV object (though it's pretty obvious that you're not).

          I think if you set the TV to its default value, the TVR will be removed the first time you save the TV. (I could be wrong).

          If the TV requires other fields (e.g., list, image, etc.) creating the TVR without them isn't going to work unless they already exist in the TV object.

          I use this method all the time, because it's very fast, but only for TVs that only need a raw value stored (which applies to almost all my TVs).
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting