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 got a Snippet pulling up some records from a custom table. I’d like to be able to search for records in this table by searching for attributes that exist in a joined table. My table has a foreign key linking it to the modUser class, and that table in turn links to the Profile class.

    $query = $modx->newQuery('myTable');
    // $query->where(array('myTable.my_column:LIKE' => '%something%'));  // <-- searching myTable works  
    $query->where(array('myTable.modUser.Profile.fullname:LIKE' => '%Bob%'));  // <-- searching the joined table does not work :(
    $my_records = $modx->getCollectionGraph('myTable', '{"modUser": {"Profile":{} } }',$query);
    


    Did I do the correct syntax for the where method?

      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: Everett at Jan 11, 2011, 12:06 AM

      I’ve got a Snippet pulling up some records from a custom table. I’d like to be able to search for records in this table by searching for attributes that exist in a joined table. My table has a foreign key linking it to the modUser class, and that table in turn links to the Profile class.

      $query = $modx->newQuery('myTable');
      // $query->where(array('myTable.my_column:LIKE' => '%something%'));  // <-- searching myTable works  
      $query->where(array('myTable.modUser.Profile.fullname:LIKE' => '%Bob%'));  // <-- searching the joined table does not work :(
      $my_records = $modx->getCollectionGraph('myTable', '{"modUser": {"Profile":{} } }',$query);
      


      Did I do the correct syntax for the where method?
      No. It’s just the "alias" of the related table class (in this case, for modUserProfile the alias is Profile), e.g. ’Profile.fullname:LIKE’ — there is no hierarchy preserved in the query aliases wrt the graph.
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Ah, thanks Jason. So for the record, the where method looks like this:

        $query->where(array('Profile.fullname:LIKE' => '%Company%'));  


        So to describe this for the layman: you use the right-most bit of the hierarchy, i.e. ONLY the alias and its attribute (Profile and fullname in this case). The relations that got us to that alias take care of themselves without being verbosely specified. I’m updating the docs.