We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18654
    • 191 Posts
    When I run the following code I get a result set that includes a bunch of empty modDocument resources (no id, date settings all at 0000-00-00, etc) rather than just static resources and I’m not sure why since I’ve entered "modStaticResource’ as the first argument. If I include class_key => ’modStaticResource’ then things work as expected but it seems like I shouldn’t need to do this. Any ideas?

    <?php
    
    $criteria = $modx->newQuery('modStaticResource', array(
    	'published' => '1'
    	,'deleted' => '0'
    	,'context_key' => 'web'
    	//,'class_key' => 'modStaticResource'
    	
    ));
    
    $staticResources = $modx->getCollection('modStaticResource', $criteria);  
    
    foreach ($staticResources as $staticResource) {  
    	echo '<pre>';
    	$recordArray = $staticResource -> toArray();
    	print_r($recordArray);
    	echo '</pre>';
    } 
    
    ?>
    

      God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
    • You should call it using the base class modResource and limit the result using the class_key criteria. Single-table inheritance in xPDO will return the proper derivative class this way, but I’ll look into making it work when calling derivative classes directly.
        • 18654
        • 191 Posts
        Thanks for the response! I have another question. I’m trying to get a collection of modResource objects and then filter them by Template Variable values.
        This is the structure that I’m attempting to use (I haven’t filled in the final logic yet, just trying to get the basics down) but it’s not working. If you could take a look, I’d appreciate it.

        <?php
        
        $criteria = $modx->newQuery('modResource', array(
        	'published' => '1'
        	,'deleted' => '0'
        	,'context_key' => 'web'
        	,'class_key' => 'modStaticResource'
        	
        ));
        
        $staticResources = $modx->getCollection('modResource', $criteria);  
        
        foreach ($staticResources as $staticResource) {  
        	echo '<pre>';
        	$relTVs = $staticResource->getMany('modTemplateVar');	
        	foreach ($relTVs as $relTV){
        		echo $relTV->get('name');	
        	}
        	//$recordArray = $staticResource -> toArray();
        	//print_r($recordArray);	
           	echo $staticResource->get('pagetitle');  
        	echo '</pre>';
        } 
        
        ?>
        


        My understanding is that the outer foreach loop iterates through modResource objects. So I’m reasoning that I should be able to call the getMany method on each of those objects to get related TV’s. Then, I can get an array of TV’s associated with each modResource object and do stuff based on those values.


          God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
        • If this is from the latest SVN, you need to use ’TemplateVars’ rather than ’modTemplateVar’; the alias’ were changed for consistency between beta-3 and the current revisions.
            • 18654
            • 191 Posts
            Actually - it was a mistake on my part. I have beta-3 but temp variables were tied to containing folder rather than the resource itself.
              God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
              • 43113
              • 8 Posts
              Is there any way to use logic operators in criteria od getCollection? We have criteria example above:
              $criteria = $modx->newQuery('modResource', array(
                  'published' => '1'
                  ,'deleted' => '0'
                  ,'context_key' => 'web'
                  ,'class_key' => 'modStaticResource'
                   
              ));

              I would like to find some like "context_key is <web> or <other> or <else>". How to do this?
                • 3749
                • 24,544 Posts
                Yes: http://rtfm.modx.com/xpdo/2.x/class-reference/xpdoquery/xpdoquery.where

                BTW, this topic is over 5 years old. If you need more information, you should start a new topic. 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