When the groupBy is tried on the repeat array_of_objects2 that outputs the same data, the group by seems to work just fine. The output prints correctly as ["dept1","dept2","dept3"]
It’s exactly the same data, while the groupBy formatter works on a repeat output (step-3, which is my current workaround), its not working directly on api returned data (expected & ideal).
Also, not sure if this issue is only with the groupBy formatter not working on api-returned “array of objects”.
I'm attaching the test server-action file (uses a public api) used in my original bug report. Clicking on "Open in Browser" should open the output of the file. groupBy.php.zip (1.3 KB)
Problem is that PHP doesn’t really have objects, sometimes they are created as keyed arrays and sometimes they derive from the stdClass. An array is accessed as $item[$prop] and a class $item->$prop. We normally use arrays internally in Server Connect, but the API seems to have returned a class instead. In the file I tried to case item as file in the foreach ($val as (array)$item), this seems not to be allowed there. So we could try to put the cast on the next line as $item = (array)$item;.
Full code for the function becomes:
function formatter_groupBy($val, $prop) {
if (!is_array($val)) return NULL;
$groups = array();
foreach ($val as $item) {
$item = (array)$item;
$key = strval($item[$prop]);
if (!array_key_exists($key, $groups)) {
$groups[$key] = array();
}
$groups[$key][] = $item;
}
return (object)$groups;
}