We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Hi;

    I have a site running and need to update it importing content from another modx site (2.1.5-pl).
    Provisioner actually doesn't work with this versions.

    I need to add around 500 new resources to the production site.
    I'm using TVs so i guess modx_site_content import in phpMyAdmin would not work.
    Suggestions?

      • 3749
      • 24,544 Posts
      TV values are stored in a table with the resource ID and TV ID. Since those wouldn't be the same in the other site, they'd be messed up.

      I think I would import the resources and make sure the TVs existed, had exactly the same name, and were connected to the right templates.

      Then I'd write a utility snippet that would save all the TV values by TV name and resource name (or alias) to a CSV file. Then write another snippet to import that CSV file and set the TV values on the new site.

      The relevant object is modTemplateVarResource.

      Something like this (untested and incomplete). You'll definitely want to add some sanity checks. There are more efficient ways to write this, but I thought this code would be clearer and it only has to run once.

      <?php
      /* Export TV Values snippet */
      
      $output = "";
      $tvrs = $modx->getCollection('modTemplateVarResource');
      
      $fp = fopen('myfile.txt', 'w');
      
      foreach ($tvrs as $tvr) {
        $tvId = $tvr->get('tmplvarid');
        $resourceId = $tvr->get('resourceid');
      
        $tv = $modx->getObject('modTemplateVar', $tvId);
        $tvName = $tv->get('name');
      
        $resource = $modx->getObject('modResource', $resourceId);
        $resourceName = $resource->get('pagetitle');
      
         $tvValue = $resource->getTVValue($tvId);
         $line = $tvName . ',' . $resourceName . ',' . $tvValue . "\n";
      
         fwrite($fp,$line);
      
         $output .= $line;
         
      }
         fclose($fp);
         /* this will put the output on the screen for debugging */
         return $output;


      <?php
      /* Import TV Values snippet */
      
      /* read each file line here into $line*/
      
      $fp = fopen("/myfile.txt", "r");
      
      while (($line = fgets($fp, 4096)) !== false) {
          $fields = explode(',', $line);
      
          $resource = $modx->getObject('modResource', array('pagetitle' => $fields[1]));
      
          $resource->setTVValue($fields[0], $fields[3]);
          $resource->save();
      }
      fclose($fp);
      
        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
      • Wow, lots of thanks for the insights and code.
        This definitively will be useful. Will make the test and let you know.

        Looking for the answer to this i found importX Extra could help me a lot if only it had a Export Tab.
        That way i could export from the local site and import in the production site. Will try at least to file it a requested feature.

        Thanks Bob.