We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24629
    • 370 Posts
    Hi how can i use a TV value in a getcollection call?

    right now i have this where 'arts' is my TV name and i want to get all resources that have the value 'de boer' .

    $criteria = $modx->newQuery('modResource');
    $criteria->where(array(
       'parent' => 33,
       'arts' => 'deboer'
    ));
    $docArray = $modx->getCollection('modResource',$criteria);


    why doesn't this return any results?

    tnx
    Rdg
    • are 'de boer'and 'deboer' kind of typing-mistake?
        • 3749
        • 24,544 Posts
        'arts' is not a resource field. Since the query is on modResource objects, only resource fields can be used in the criteria.

        Can you get what you want with getResources? It will allow you to use TV values in the search. Otherwise, I think you need to do a join with the table containing the modTemplatevarResource objects. That table holds the TV values for specific resources.

        Another (faster) way is to either put the 'art' info into an unused resource field or use a custom DB table for the data.
          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
          • 24629
          • 370 Posts
          Hi
          @ nephews yes its a typo but just in this example

          @BobRay: tnx for the book it arrived today:). Yes i can get what i want with get resources but i'm trying to learn how the basic functions work so how would i do this within a snippet?
          Or can i use getResources in a snippet aswell? if yes, how?

          tnx
          Rdg
            • 3749
            • 24,544 Posts
            Thanks for buying the book. smiley

            Take a look at the source code of getResources to see how to search with the TV values. BTW, this kind of search is particularly inefficient because the TV fields are not indexed for that kind of searching.

            If you have a search-intensive operation, you'd be better off either using an unused resource field for the search criteria or a custom DB table with indexes on the search fields.

            I should mention that you can also use a brute-force method where you get the resources with getCollection(), regardless of TV value, then check the TV value for each one with:

            $resource->getTVValue($TvId);

            filtering out the ones you don't want.

            And . . . yes, you can use getResources in a snippet using
            $modx->runSnippet('getResources', $props)


            where $props is an array containing the property keys and values that you would have put in the snippet tag.

            Remember, though, that what you'll get back from getResources is not an array of resources, it's the processed output of the getResources call using the Tpl chunk -- IOW, what you'd see on the page if you had a getResources tag there. That's probably not what you want. [ed. note: BobRay last edited this post 12 years, 5 months ago.]
              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
              • 3749
              • 24,544 Posts
                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
                • 24629
                • 370 Posts
                Thanx BobRay,

                The brute-force method will be the weapon of my choice.

                Rdg