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?