We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21884
    • 57 Posts
    i’ve searched for hours now, but i can’t figure out why this returns a blank page (no error, none in modx, none in my logs nothing)

    	$c = $modx->newQuery('modTemplateVarResource');
    	$c->select('DISTINCT `value`');
    	$c->where('`tmplvarid` = 30');
    
    	$collection = $modx->getCollection('modTemplateVarResource', $c); // script seems to choke here
    
    	foreach($collection as $item) {
    		$test =  $item->get('value')."\n";
    		echo $test;
    	}


    obviously i’ve got something wrong but what is it? (i haven’t been able to find anything on what getCollection actually returns, and all examples use modResource, in this case i’m just trying to get a list of values from any db table, i figured since it’s a modx table i should be able to get it without defining a model/schema etc... am i incorrect in that thought?)
      • 28215
      • 4,149 Posts
      Your code should be this:
      $c = $modx->newQuery('modTemplateVarResource');
      $c->select('DISTINCT '.$modx->getSelectColumns('modTemplateVarResource','','',array('id','value')));
      $c->where(array(
            'tmplvarid' => 30,
      ));
      $collection = $modx->getCollection('modTemplateVarResource', $c);
      


      You have to include the PK of a table (here, "id") to grab it as an object. You could do a straight PDO query, but you wouldn’t get the xPDOObject of modTemplateVarResource.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 21884
        • 57 Posts
        fantastic, thanks for your help!