How to get all Collections with getCollection?


I’m trying to get all resources that are a ‘Collection’. (from the collections extra)

Step to reproduce

Strangely enough running this code
$docs = $modx->getCollection(‘CollectionContainer’);
foreach ($docs as $doc){
$o .= $doc->get(‘id’);
returns ALL pages in the context not just the ones that have class_key CollectionContainer.
Any one know why that is?


My guess is that it’s because that class extends the modResource class, so without giving it any criteria, it’s getting all modResource objects.

Try this:

$docs = $modx->getCollection(‘CollectionContainer’, array('class_key' => 'CollectionContainer'));

Another way to go would be to use the id of the container (assuming that there are no “grandchildren” of the container):

$containerId = 12; // id of the parent container
$docs = $modx->getCollection(‘CollectionContainer’, array('parent' => $containerId));

you’re right
$docs = $modx->getCollection(‘CollectionContainer’, array('class_key' => 'CollectionContainer'));
works. But i find it not logical. Whats the use of using a class if getCollection will use its Super ?

tnx for the answer

I don’t really know why that’s the case, but I’ve made that mistake myself.

I think it has to do with the architecture of xPDO. Even though modDocument and Article are classes in their own right, queries for them always seem to look for modResource objects.