Quote from: leaase at May 16, 2010, 05:26 AM
If you see any optimization possibilities please let me know 
Here is an optional approach that is slightly more optimal. Since you don’t actually use the parent Resource, no reason to query and load it from the database. Also, if you select() only the columns you need from the object, you optimize the query and the amount of memory used by each object.
<?php
/* build a query to get Resources with a parent of 2 that are published and not deleted */
$query = $modx->newQuery('modResource', array('parent' => 2, 'deleted' => false, 'published' => true));
/* to optimize, you can select only the columns you need */
$query->select($modx->getSelectColumns('modResource', 'modResource', '', array('id', 'class_key', 'context_key')));
/* get the modResource objects with a parent of 2 */
$children = $modx->getCollection('modResource', $query);
/* loop through the children and push the id onto $resourceIds */
$resourceIds = array();
foreach ($children as $child) {
$resourceIds[] = $child->get('id');
}
?>
Quote from: leaase at May 16, 2010, 05:26 AM
Is there a list of parameters that the getMany() function will accept? Why do I need to write ’Children’ with a capitalized ’C’?
It takes whatever relation
aliases are defined for the object you are working with, or rather, all the ones with a cardinality of
many. In this case, modResource defines the following relationships to other objects in the MODx model:
<aggregate alias="Parent" class="modResource" local="parent" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Children" class="modResource" local="id" foreign="parent" cardinality="many" owner="local" />
<aggregate alias="CreatedBy" class="modUser" local="createdby" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="EditedBy" class="modUser" local="editedby" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="DeletedBy" class="modUser" local="deletedby" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="PublishedBy" class="modUser" local="publishedby" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Template" class="modTemplate" local="template" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="TemplateVars" class="modTemplateVar" local="id:template" foreign="contentid:templateid" cardinality="many" owner="local" />
<aggregate alias="TemplateVarTemplates" class="modTemplateVarTemplate" local="template" foreign="templateid" cardinality="many" owner="local" />
<aggregate alias="ContentType" class="modContentType" local="content_type" foreign="id" owner="foreign" cardinality="one" />
<aggregate alias="Context" class="modContext" local="context_key" foreign="key" owner="foreign" cardinality="one" />
<composite alias="TemplateVarResources" class="modTemplateVarResource" local="id" foreign="contentid" cardinality="many" owner="local" />
<composite alias="ResourceGroupResources" class="modResourceGroupResource" local="id" foreign="document" cardinality="many" owner="local" />
<composite alias="ResourceKeywords" class="modResourceKeyword" local="id" foreign="content_id" cardinality="many" owner="local" />
<composite alias="ResourceMetatags" class="modResourceMetatag" local="id" foreign="content_id" cardinality="many" owner="local" />
<composite alias="Acls" class="modAccessResource" local="id" foreign="target" owner="local" cardinality="many" />
The relationships defined with a cardinality of one would be loaded using $resource->getOne(), e.g.
<?php
$resource->getOne('Context');
?>
Also note that you can access the related objects loaded with getOne()/getMany() directly from the $resource:
<?php
if ($resource->getOne('Context')) {
print_r($resource->Context->toArray());
}
if ($resource->getMany('Children')) {
foreach ($resource->Children as $child) {
print_r($child->toArray());
}
}
?>