We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17230
    • 35 Posts
    Hi,

    I’m trying to create a simple snippet in modx Revolution that filters documents by the value of a custom TV and returns the amount of documents found.

    Let’s start with the Snippet Call:
    [[!categoryCount? &criteria=`business`]]


    And this is the (very basic) Snippet categoryCount - which works, as long as I’m accessing ressource fields like this:
    <?php
    	$documents = $modx->getCollection('modResource',array(
    	    'longtitle' => $criteria
    	));
    	echo count($documents);
    ?>


    But: This does not work with custom TemplateVars. When I’m trying to access those with:
    'blog_category' => $criteria

    the snippet breaks.

    Any hints how to access Custom TV’s? Thanx for your help!

    Paul
      - Just because you&#39;re not paranoid doesn&#39;t mean they&#39;re not after you -
    • getResources does this for TV’s with actual values but does not work for TV’s with @ bindings or other dynamic values (e.g. @INHERIT or @EVAL or [[Snippet]]. In order to accomplish that, you would have to get all Resources, iterate over them, process the TV for each Resource, and filter them out with PHP code (versus in a SQL query).

      Here is an example getResources call that does this:
      [[getResources? &tvFilter=`blog_category==business`]]


      So you can check out the getResources code to get some hints on how you might do this.

      Further, if all your snippet is doing is getting a count of records, you should use getCount() instead of getCollection(), e.g.
      <?php
      $count = $modx->getCount('modResource', array(
              'longtitle' => $criteria
          )
      );
      return $count;
      ?>

      Otherwise, you are sending a large amount of unnecessary data down the pipe between MySQL and PHP, as well as using a large amount of unnecessary memory and CPU resources in PHP hydrating the result set into modResource objects.