We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5789
    • 21 Posts
    I’m trying to migrate some old snippet code into the new API. I’ve successfully set it up where I can retrieve/query my custom object. The problem I’m running into is when I attempt to reference a core Modx object from my custom object. Specifically, I’m trying join my xPDOQuery on the modUser object. I don’t get any errors, it just doesn’t do anything.

    Here’s my code:

    $base_path = MODX_CORE_PATH . 'components/foobar';
    	$modx->addPackage('myFooBar',$base_path.'/model/','');
    	
    	$query = $modx->newQuery('Foo');
    	$query->innerJoin('Users','u', array('user_fk' => 'u.id')); // tried modUser vs Users and it didn't work
    	$query->select($modx->getSelectColumns('Foo','Foo','',
    						  array(
    							 'fooid',
    							 'fooDescription',
    							 'u.username',
    							 'user_fk',
    							 'fooContent',
                                                             'fooPublic'
    						  )
    					   )
    				 );*/
    	$query->where(array(
    	 'fooDescription:!=' => '',
    	 'fooPublic' => 0
    	));
    	$query->sortby('fooPublic','DESC');
    	$query->limit(20,0);
    	$results = $modx->getCollection('Foo',$query);
    	foreach($result as $res){
    	  //$username = $modx->getObject('modUser', $res->get('user_fk'))->username;  // <-- this works, but I need it in the query
    	  echo $res->get('u.username').'+ '.$res->get('fooDescription').'<br />'; // <-- this does not return the username
    	}
    
    


    I can get what I need, but that’s resorting to calls within a loop which is obviously not efficient.
      --
      Modx 2.0.2
      Win 7 Firefox 3.6.10
      Mac Snow Leopard Firefox 3.5.12
      • 22303 MODX Staff
      • 10,725 Posts
      You cannot use bindings with join expressions — IOW this line:
      $query->innerJoin('Users','u', array('user_fk' => 'u.id'));

      should look like:
      $query->innerJoin('Users','u', array("Foo.user_fk = u.id"));

      Also, getSelectColumns does not work for columns from multiple tables — you must add columns using getSelectColumns only from the class being specified. Use separate calls to specify the columns for each table:
      $query->select($modx->getSelectColumns('Foo','Foo','', array(
          'fooid',
          'fooDescription',
          'user_fk',
          'fooContent',
          'fooPublic'
      )));
      $query->select($modx->getSelectColumns('Users','u','', array(
          'username',
      )));
        • 5789
        • 21 Posts
        Ok. Got the join to work. My join join syntax was off. Worked like a charm. Now I have another problem. How does one alias a column? Say I wanted to return the fooDescription as simply "TheDescription"?

        How does "SELECT fooDescription as TheDescription FROM foos" translate into xPDO?
          --
          Modx 2.0.2
          Win 7 Firefox 3.6.10
          Mac Snow Leopard Firefox 3.5.12
          • 28215
          • 4,149 Posts
          $c = $modx->newQuery('Foo');
          $c->select(array(
             'TheDescription' => 'fooDescription',
          ));
          
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 5789
            • 21 Posts
            Thank you sir
              --
              Modx 2.0.2
              Win 7 Firefox 3.6.10
              Mac Snow Leopard Firefox 3.5.12