Query MIGX Package - how to sort getCollectionGraph results?

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]);


// class
$class = 'product';

$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);

// $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

I think this should work:

$c = $modx->newQuery('product');
$c->where(["published" => 1, "option.published" => 1]);
$products = $modx->getCollectionGraph('product', '{"option":{}}', $c);

Perfect, thank you.

I updated it to order the product options by pos (asc) but that was a trivial adjustment, you provided the heavy lifting.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.