We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21792
    • 37 Posts
    In MODx 2.0.x I've been using $modx->getDocument to do a whole range of things in my snippets, IMO it's the the most useful method supplied in $modx.

    Ever since I've staretd using it, I know it's deprecated, to be removed in the 2.1 branch, but the documentation has never suggested an alternative....

    So what is the alternative?

    90% of my snippets reply on being able to access the data within the modx_site_content table for any given id.

    Any suggestions?
      • 28215
      • 4,149 Posts
      $resource = $modx->getObject('modResource',$id);
      $pageTitle = $resource->get('pagetitle');
      /* etc */
      


      http://rtfm.modx.com/display/xPDO20/Retrieving+Objects
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 33968
        • 863 Posts
        Check out:
        /core/model/schema/modx.mysql.schema.xml

        That will give you an indication of the relationships between modx objects. Now you can do a hundred more things I bet you never knew existed smiley
          • 22303 MODX Staff
          • 10,725 Posts
            • 3749
            • 24,544 Posts
              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
              • 21792
              • 37 Posts
              Thanks all!

              I hadn't checked this thread for a while - I was expecting an email alert for replies (bad habit from other forums I think)

              @BobRay: Is this covered in your new book?

              Next problem, similar but related

              I've also been using $modx->getTemplateVars('*','*',$id);

              any documentation on a replacement for this?

              many thanks
                • 3749
                • 24,544 Posts
                Yes, most of the deprecated functions are listed in the MODX Object Reference in the Appendix with suggested alternatives.

                Here's the section on that one (the first one is free) wink


                getTemplateVars([array $idnames = array()], [$fields = "*"],
                [$resourceId = ""], [$published = 1], [$sort = "tv.rank,
                tvtpl.rank"], [$dir = "ASC,ASC"]) — Deprecated. Use $modx->getObject() and $obj->getMany('TemplateVars').

                Example:
                $obj = $modx->getObject('modResource', array('name'=>'MyDoc');
                $id = $obj->get('id');
                $tvs = $obj->getMany('TemplateVars');
                foreach ($tvs as $tv) {
                    $rawValue = $tv->getValue($id);
                    $processedValue = $tv->renderOutput($id);
                }
                


                For the current resource, it would be this:

                $tvs = $modx->resource->getMany('TemplateVars');
                foreach ($tvs as $tv) {
                    $rawValue = $tv->getValue($modx->resource->get('id'));
                    $processedValue = $tv->renderOutput($modx->resource->get('id'));
                }
                
                [ed. note: BobRay last edited this post 14 years, 10 months ago.]
                  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
                  • 21792
                  • 37 Posts
                  Many thanks @BobRay!
                    • 21792
                    • 37 Posts
                    my replacement:

                    instead of

                    $doc = $modx->getDocument($id);
                    $TVs = $modx->getTemplateVars('*','*',$id);


                    I now have to retrofit this into my existing sites if I want to upgrade:

                    $rs = $modx->getObject('modResource', $id);
                    $t = $rs->getMany('TemplateVars');
                    
                    $TVs = array();
                    
                    foreach($t as  $tv) {	
                    	$name = $tv->_fields['name'];
                    	$value = $tv->_fields['value'];
                    	$TVs[$tv->_fields['name']]['value'] = $tv->_fields['value'];    
                    }
                    
                    
                    $els = array('pagetitle','introtext'); //find which elements i used and populate this
                    
                    foreach($els as $el) {
                    	$doc[$el] = $rs->get($el);
                    }


                    really seems a step backwards

                      • 22303 MODX Staff
                      • 10,725 Posts
                      Why are you getting any Template Variables if you are only getting pagetitle and introtext? Neither of those are Template Variables. Also, that code is definitely not how to replace the original API calls and I'm not sure how you came up with that...in fact, those original API calls look to be extreme overkill for collecting two field values from a Resource anyway. Or perhaps you did not share some key point of your code?