We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53432
    • 46 Posts
    I have a snippet that gets resources, changes their content, then saves them.

    In the code below, would $resource->save() honor the existing values of the TV’s `foo` and `bar` … or would `foo` and `bar` be reset to their defaults for the resource being saved?

    $c = $modx->newQuery('modResource');
    $query_params = array(
    	'parent'      => 42,
    );
    $c->sortby('pagetitle','ASC');
    $c->where($query_params);
    $total = $modx->getCount('modResource',$c);
    $resources = $modx->getIterator('modResource',$c);
    $n=0;
    foreach ($resources as $resource) {
    	$id         = $resource->get('id');
    	$foo        = $resource->getTVValue('foo');  // get a TV just to see if it’s set
    	$bar        = $resource->getTVValue('bar');  // get a TV just to see if it’s set
    // do some work on the content here…
    	$my_content = 'whatever…'; // for example
    // update $resource:
    	$resource->set('content',$my_content); // update the content
    	$resource->set('menuindex',$n);
    // save the updated resource:
    	$resource->save();
    	$n++;
    }
    


    This question has been answered by donshakespeare. See the first response.

      • 42562
      • 1,145 Posts
      donshakespeare Reply #2, 7 years ago
      Do a test and confirm.

      But I see no reason saving a resource would/should meddle (unexpectedly/negatively) with its TV values.
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 53432
        • 46 Posts
        Tested, and it seems to have no effect on TVs. I was hoping someone with deeper knowledge of xPDO would confirm. I found no specific documentation. Honestly I wouldn’t think so, either. Just wanted to confirm.

        For context: I’ve worked with WordPress and if you save a post but do not submit all your custom fields simultaneously, then WordPress assumes your intention was to delete those custom fields, which it does. Hence my overly cautions approach with this!
        • discuss.answer
          • 42562
          • 1,145 Posts
          donshakespeare Reply #4, 7 years ago
          A resource and its native fields head to one table.
          Saving a resource will never, by default, crisscross into another table to affect custom fields, of which fields TV is one.

          You will need another script to "access" / connect to the appropriate TV table in order to revert/update TVs values attached to a resource (via template)

          Getting and setting
          https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/accessing-template-variable-values-via-the-api

          Awesome resource!
          https://bobsguides.com/blog.html/2016/11/14/understanding-the-modtemplatevarresource-object/

          Nice schema of things
          https://github.com/modxcms/revolution/blob/2.x/core/model/schema/modx.mysql.schema.xml
            TinymceWrapper: Complete back/frontend content solution.
            Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
            5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
            • 3749
            • 24,544 Posts
            $resource->save won't touch any TV values. You can include them in the fields when you call $modx->runProcessor('resource/update'), 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
              • 53432
              • 46 Posts
              Thank you @BobRay and @donshakespeare! Exactly what I needed to know!