We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53432
    • 46 Posts
    I am having trouble understanding how to combine AND logic with $modx->newQuery->innerJoin().

    Is it possible to use $modx->newQuery to get all resources that match the following conditions:

     `parent`= 3 
     AND `template`= 3
     AND `published`= 1
     AND `deleted`= 0
     AND `tv1`= 1
     AND `tv2` LIKE '%IA%'
    


    (“tv1” and “tv2” are Template Variables named “isFeatured” and “FeaturedRegions”, respectively)

    Can this be done in a single query?

    My only other idea here would be to ignore the TV’s in the query. I could simply match all the non-TV conditions, then, iterate through the results and filter them. Which seems like it would be slow and cumbersome. Any suggestions would greatly be appreciated!

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Sort of, but it's tricky. You need to join the modTemplateVarResource object, but it only holds raw TV values that don't match the default value of the TV. In other words, it will only work if none of the values for the TV match the TV's default value, *and* you can use the raw value of the TV rather than the processed value.

      This seems impossible, but it's true: If the TV's value for a resource matches the TV's default value, there is no record for that TV in the modTemplateVarResource table (or anywhere else). So to find the value of a TV for a given resource if it might be set to the default value, you have to check the modTemplateVarResource table, and if there's no record, query the modTemplateVar table to get the TV object and retrieve its default value -- or call $resource->getTVValue() and have it do all that for you. Obviously, this can't be done with a single query.

      If there are default values set, you need a brute force approach where you fall back to the check the TV's default value if there's no record in the modTemplateVarResource table.

      Take a look at the TvFilters code in getResources and/or pdoResources. (I would be tempted to just use one or the other), though you can improve on it if you can use raw values and don't have to worry about default values.

      If (and it's a big if) you can shoehorn the values in your two TVs into unused resource fields (e.g., longtitle, introtext, menutitle), the problem becomes trivial and you can do a very fast search in a single query.

      Another (better, imo) option is ClassExtender, which allows you to create a custom DB table containing the extra data fields (replacing the TVs), and adds input fields for them to the Create/Edit Resource panel.

      With ClassExtender, and extra fields called Tv1 and Tv2, you get to do something like this (with custom Tpl chunks if necessary):

      [[!GetExtResources?
          &where=`{"Tv1:=":"1","Tv2:LIKE":"%IA%", "Resource.published:=":"1","Resource.deleted:=":"0",Resource.parent:=":"2",Resource.template:=":"3"}`
          &sortby=`pagetitle`
          &sortDir=`ASC`
      ]]


      Easy Peasy.

        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
        • 53432
        • 46 Posts
        @BobRay – thanks! As always, that’s super helpful! I’m checking out ClassExtender, it looks to be exactly the answer I need.
          • 3749
          • 24,544 Posts
          Don't thank me until it works. wink
            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