For comparison, here is a Snippet I created called getValue that can get a single field from any xPDOObject derivative class (i.e. any tables represented by an xPDO model), using JSON to describe the criteria. It does not work as a filter, but this is a very efficient way to get a single field from an object based on a set of criteria, and optionally provide a default value if nothing is returned by the query:
<?php
$value = !empty($default) ? $default : '';
if ($criteria = $modx->newQuery($class, $modx->fromJSON($where))) {
$pk = $modx->getPK($class);
if (!is_array($pk)) {
$pk = array($pk);
} else {
$pk = array_values($pk);
}
$criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, array($field))));
if ($object = $modx->getObject($class, $criteria)) {
$value = $object->get($field);
}
}
return $value;
?>
The key difference is instead of selecting every single column of the table and returning all that data across the pipe from MySQL to create the object, here I am only selecting the requested field (and the primary key field( s ), which is required of any xPDOObject query). This reduces both the amount of time it takes to query and pass the data from the MySQL server, and the amount of memory used by PHP when that data is returned.
Also, the fromJSON() allows the representation of just about any criteria when requesting the data. Some examples:
[[getValue? &class=`modUser` &field=`username` &where=`{"id":[[+createdby]]}` default=`anonymous`]]
[[getValue? &class=`modResource` &field=`content` &where=`{"id":[[+id]]}`]]
[[getValue? &class=`modResource` &field=`id` &where=`{"alias":"foobar","isfolder":1}`]]