We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50105
    • 13 Posts
    I'm trying to output tv values alongside modResource using read function with RESTful APIs, on top of MODX.

    Here is my code so far.

    <?php
    class myControllerPosts extends modRestController{
        public $classKey = 'modResource';
        public $defaultSortField = 'id';
        public $defaultSortDirection = 'DESC';
    	
    	
        public function read($id) {
            if (empty($id)) {
                return $this->failure($this->modx->lexicon('rest.err_field_ns',array(
                    'field' => $this->primaryKeyField,
                )));
            }
            /** @var xPDOObject $object */
            $c = $this->getPrimaryKeyCriteria($id);
            $this->object = $this->modx->getObject($this->classKey,$c);
    		
            if (empty($this->object)) {
                return $this->failure($this->modx->lexicon('rest.err_obj_nf',array(
                    'class_key' => $this->classKey,
                )));
            }
    		
    	$tvID = 17;
    	$tv = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvID, 'contentid' => $id));
    		
    		
    				
    $objectArray = Array();
    $objectArray ['id']  = $id;
    $objectArray ['pagetitle']  = $this->object->pagetitle;
    $objectArray ['alias']  = $this->object->alias;
    		
    			
            $objectArray ['someTV'] = $tv->value;
    		
    
            $afterRead = $this->afterRead($objectArray);
            if ($afterRead !== true && $afterRead !== null) {
                return $this->failure($afterRead === false ? $this->errorMessage : $afterRead);
            }
    
            return $this->success('',$objectArray);
        }
    
       
    }


    The result I'm getting when I visit my url mydomain/rest/posts/6 is:

    {"message":"","object":{"id":"6","pagetitle":"someTitle","alias":"someAlias","someTV":null},"success":true}



    I have done the same for modUser and was able to output modUserProfile.

    Are the TV objects accessible from this class?

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

      • 3749
      • 24,544 Posts
      Try this:

      $objectArray ['someTV'] = $tv->get('value');
        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
        • 50105
        • 13 Posts
        Hi Bob,

        I previously tried that and it gave me 500 server error.

        When I output the value of $tv it's null also.
        • discuss.answer
          • 3749
          • 24,544 Posts
          Is the TV set to the default value? In that case, there's no modTemplateVarResource record and your code would fail.

          This will be a tiny bit slower, but should be much more reliable:

          $objectArray['someTV'] = $this->object->getTVValue($tvID);


          That way you don't need to get the modTemplateVarResource object or worry about it not existing.
            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
            • 50105
            • 13 Posts
            Thanks BobRay that worked.
              • 50105
              • 13 Posts
              Thanks BobRay that worked.
                • 3749
                • 24,544 Posts
                I'm glad I could help. smiley
                  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