Can anyone help me sort the results returned by a getCollectionGraph query?
The following was provided by @halftrainedharry in a recent post regarding table joins.
$products = $modx->getCollectionGraph('product', '{"option":{}}', ["published" => 1, "option.published" => 1]);
This returns product
ordered by id with option
also ordered by id.
However I need product ordered by name (asc) and option ordered by pos (asc)
I tried adjusting Harry’s getCollectionGraph query from
$products = $modx->getCollectionGraph('product', '{"option":{}}', ["published" => 1, "option.published" => 1]);
to
// class
$class = 'product';
//criteria
$c = $modx->newQuery($class);
// example: $crit->bindGraph('{"modUserProfile":{"internalKey":{}}}');
$c->bindGraph('{"option":{}}', ["published" => 1, "option.published" => 1]);
$c->where('published', 1);
$c->where('option.published', 1);
$c->sortby('name','ASC');
$c->sortby('option.name','ASC');
// $products = $modx->getCollectionGraph($class, $c); // produces same output as line below
$products = $modx->getCollectionGraph($class, '{ "option":{} }', $crit);
// print_r($products);
echo 'SQL: ' . $c->toSql(); // empty
but the long form version produces the same result.
And for an unknown reason $c->toSql();
is empty.
Can anyone suggest how to achieve the sort order I need please?
Resorces I’ve looked at so far include
- Filtering results with getCollectionGraph | MODX Community Forums
- But I don't WANT to order by primary key | MODX Community Forums
- custom sort order for getCollection | MODX Community Forums
- getCollectionGraph - Retrieving Objects | MODX Documentation
- xPDO.getCollection - xPDO Class | MODX Documentation
- How to learn advanced MIGX capabilities?
- xPDO.query - xPDO Class | MODX Documentation