We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    I’ve set up a query that’s returning a 974 results, (via the getCount) but when I sort the results by the column in a related table, getCount returns a value of 0. What gives?

    $query = $modx->newQuery('Hgids');
    $query->sortby('modUser.username', 'ASC');
    
    $query->where( array('Hgids.hgid:LIKE'=>"%$search_term%") );
    $query->orCondition(array('modUser.username:LIKE' => "%$search_term%"));  
    $query->orCondition(array('Profile.fullname:LIKE' => "%$search_term%"));
    $total_records = $modx->getCount('Hgids',$query);  // <-- this fails when the sortby is 'modUser.username'
    
    $query->limit(100, 0);
    
    $hgids = $modx->getCollectionGraph('Hgids', '{"modUser": {"Profile":{} } }',$query);
    


    Why would getCount work in one situation and not in another? Is there something I should be doing differently there?
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Hmm... I think I figured out my own question, but I wonder if Jason can explain why:

      $query = $modx->newQuery('Hgids');
      $query->sortby('modUser.username', 'ASC');
      
      $query->where( array('Hgids.hgid:LIKE'=>"%$search_term%") );
      $query->orCondition(array('modUser.username:LIKE' => "%$search_term%"));  
      $query->orCondition(array('Profile.fullname:LIKE' => "%$search_term%"));
      
      $query->bindGraph('{"modUser": {"Profile":{} } }');	// without this, sorting by modUser.username causes getCount to fail.
      
      $total_records = $modx->getCount('Hgids',$query);  
      
      $query->limit(100, 0);
      
      $hgids = $modx->getCollectionGraph('Hgids', '{"modUser": {"Profile":{} } }',$query);


      See that? The call to bindGraph right before getCount fixes the problem I was having, but I can’t explain why.

      Hope that helps somebody.
        • 28215
        • 4,149 Posts
        Yeah, for some reason you cant bindGraph before a getCount call. Try this instead:

        $query = $modx->newQuery('Hgids');
        $query->innerJoin('modUser','modUser');
        $query->innerJoin('modUserProfile','Profile','Profile.internalKey = modUser.id');
        $query->where(array(
            'Hgids.hgid:LIKE'=>"%$search_term%",
            'OR:modUser.username:LIKE' => "%$search_term%",
            'OR:Profile.fullname:LIKE' => "%$search_term%",
        ));
        $total_records = $modx->getCount('Hgids',$query);
        
        $query->bindGraph('{"modUser": {"Profile":{} } }');
        $query->sortby('modUser.username', 'ASC');
        $query->limit(100);
        $hgids = $modx->getCollectionGraph('Hgids', '{"modUser": {"Profile":{} } }',$query);
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Putting bindGraph before the getCount made it work. If I move the bindGraph after the getCount, then getCount returns zero, so my second post has working code in it.
            • 28215
            • 4,149 Posts
            Oh, prob b/c the where stmt has no table to reference the Profile joined table to.
              shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              There really needs to be rock solid docs for stuff like this.... I’d write a wiki page, but I’m stumbling in the dark for half of this stuff.