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

    I’ve been doing a lot recently with xPDO to do with building paginated lists of data from various xPDO models. One of the things when generating these lists is to find the total number of records to be returned before splicing it to only return the required records/objects for the list ’page’ being viewed.

    Now, this is fairly easy to accomplish when using standard collections (i.e. using getCollection) because I can pass the criteria into getCount() before I add the limit information to the criteria and pass it into getCollection(). For example:
    $c = $modx->newQuery('modDocument');
    $c->where(array('content:LIKE' => '%mySearch%'));
    
    $count = $modx->getCount('modDocument', $c);
    
    $c->limit(20, 0);
    $pagedCollection = $modx->getCollection('modDocument', $c);


    However, if I want to use a collection graph, then getCount fails miserably (the following returns 0 for the count):
    $c = $modx->newQuery('modDocument');
    $c->bindGraph('{"modTemplateVarResource":{"modTemplateVar":{}}}');
    $c->where(array('content:LIKE' => '%mySearch%'));
    
    $count = $modx->getCount('modDocument', $c); /* now returns 0 */
    
    $c->limit(20, 0);
    $pagedCollection = $modx->getCollectionGraph('modDocument', '{"modTemplateVarResource":{"modTemplateVar":{}}}', $c);


    Is there a recommended way of getting counts like this when using getCollectionGraph? Up to now, I’ve found the only way to do this is to generate the SQL query from the criteria and then force that into a normal PDO statement - this just seems a really fudgy way to do it. Maybe a getCountGraph() method could be added to alleviate this issue?
      Garry Nutting
      Senior Developer
      MODX, LLC

      Email: [email protected]
      Twitter: @garryn
      Web: modx.com
    • Quote from: garryn at Feb 13, 2009, 04:02 PM

      Is there a recommended way of getting counts like this when using getCollectionGraph? Up to now, I’ve found the only way to do this is to generate the SQL query from the criteria and then force that into a normal PDO statement - this just seems a really fudgy way to do it. Maybe a getCountGraph() method could be added to alleviate this issue?
      Yes, a getCountGraph() or just making getCount() a little smarter is probably in order there. The challenge is to consider that whenever joins on one-to-many related objects are involved, the row count is the maximum number of child objects returned for each parent.

      Could you enter a JIRA ticket for this improvement?
        • Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com