We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32678
    • 290 Posts
    Trying to filter and match multiple TVs with getResources and have hit a snag...

    A TV value is set on a template (call it Template1) as either 0 or 1. For this example, call this TV [[show]].

    For any resource where [[show]]==1, I want to also compare two more TVs. The first is also associated to Template1. Call it [[category1]].

    The second TV -- call it [[category2]] -- is associated to Template2.

    So I want this: IF [[show]]==1 AND [[category1]]==[[category2]] THEN [[myChunk]]. This request is on a resource that doesn't use any aforementioned TV.

    I've combined the match for [[show]]==1 with [[category1]]==[[category2]] in getResources tvFilters, but with null results. If I remove the second filter ([[category1]]==[[category2]]), I get results, so it's the second comparison that isn't right.
    [[!getResources:default=`Null`?
     &parents=`2` // container for all resources assigned Template1 or Template2
     &where=`{"template:IN":[1,2]}`
     &includeTVs=`1`
     &includeTVList=`show,category1,category2`
     &tvFilters=`show==1,category1==category2`
     &tpl=`myChunk`
     &limit=`1`
     &sortby=`RAND()`
    ]]


    I've played with adding a wildcard to the second match in tvFilters, but with no luck. Is there something incorrect here that I'm missing?

    P.S. I'm doing this in the first place because [[myChunk]] uses template tags from resources using both Template1 and Template2, hence the need for the comparisons.
      • 3749
      • 24,544 Posts
      The code looks right to me. Are you sure there's a published, not hidden from menus, resource that's a child of resource 2, has a template of 1 or 2, has the show TV set to 1, and has a TV named category1 set to the value 'category2' (without the quotes)?

      Are you sure that the current user has access to both the resources and the TVs? In the front end, you're the anonymous user unless you log in there, even when previewing from the Manager.

      Do the TVs have a tv. prefix? If not, you may need &tvPrefix=``.
        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
        • 32678
        • 290 Posts
        Thanks Bob. Yes on all counts. In fact, I have a getResources call using almost the same approach elsewhere and it returns results. It looks for a match between category1 and category2. The difference is that one of the two TVs being compared is on the template that holds the call.

        I think getResources won't compare values between two TVs as I seek to do here.

        In the actual call, show==1 and category1=%myTag% in &tvFilters returns a result. "myTag" is a value within both the category1 and category2 TVs.

        Swapping category2 for category1, without changing anything else, returns null; removing show==1 and keeping category2==%myTag% returns a result.

        I also removed show==1 one from tvFilters and kept only category1==category2, and this returns null. Hence, I assume that at least one of the TVs being compared needs to be on the template that holds the call.

        What I need is to first search for resources that use template1 with show==1, and then find matches between the two templates for category1 and category2.

        I don't think I can do this with extras, though. Would you agree? [ed. note: wpicotte last edited this post 10 years, 3 months ago.]
          • 3749
          • 24,544 Posts
          The connection between the TVs and the current page's Template shouldn't matter, but you could try just connecting both TVs to that template to see if it makes a difference. Clear the cache before testing.

          You might also be able to do it by adding the TV comparisons to the &where property, although I'm not sure about the form. This might be it:

          &where=`{"template:IN":[1,2],"show":"1","category2":"category1"}`


          Another thing to try would be pdoResources. If it works it should be faster than getResources.

          One more thought, since TVs are returned as strings, you might try '1' for the numeric comparison.

          I know a number of people have had trouble with complex TVFilters operations and some never found a solution.
            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
            • 32678
            • 290 Posts
            OK, thanks. For whatever reason, getResources cannot match the two category TVs. If I strip everything out and simply try to get an output on that alone, I get Null.
              • 4172
              • 5,888 Posts
              you say:
              the TV category1 is associated to resources with template1
              and
              the TV category2 is associated to resources with template2

              how can there be resources, where the value of category1 is equal to the value of category2, if a resource has only one of them associated?
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 32678
                • 290 Posts
                Quote from: Bruno17 at Jun 24, 2016, 02:49 AM
                how can there be resources, where the value of category1 is equal to the value of category2, if a resource has only one of them associated?

                In brief, the values used by the two TVs are tags, and both TVs store identical data. In conjunction with a custom snippet, they're used to dynamically build sibling navigation based on tags, not the site tree.