We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42502
    • 12 Posts
    Hi
    i have to update TV values in multiple documents form collection. I'm trying to do it by this piece of code but ... it stops after first loop?
    can anybody hep me?

    foreach($collection as $item) {
        $page = $modx->getObject('modResource', $item->get('contentid'));
        $page->setTVValue('Country', 'test');
    }
    
      • 42502
      • 12 Posts
      solved!
      i have some mess in DB and have some TV values with contentid's of non existing pages? is there any easy and safe way to clean it?
        • 3749
        • 24,544 Posts
        Any idea how that happened?

        I think this would clean them up (Back up the DB first! and clear the site cache before running this) -- this is off the top of my head and untested:

        <?php
        $tvResources = $modx->getCollection('modTemplateVarResource');
        $count = 0;
        foreach($tvResources as $tvResource) {
           $docId = $tvResource->get('contentid');
           $doc = $modx->getObject('modResource', $docId;
           if (! $doc) {
               $tvResource->remove();
               $count++;
           }
        
        }
        
        return ('Removed ' . $count . ' TemplateVarResources');
          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