Help with LeftJoin and grouping results from joined table

2 posts by 2 authors in: Forums > CMS Builder
Last Post: January 4, 2022   (RSS)

By daniel - January 4, 2022

Hi Jeff,

This might be a scenario where the CMSB function array_groupBy() can help out. It can be used like this:

$itemsByTitle = array_groupBy($campaign_itemRecords, 'title', true);
foreach ($itemsByTitle as $title => $groupedRecords) {
  echo 'Title: ' . $title;
  echo '<br>';

  foreach ($groupedRecords as $groupedRecord) {
    echo $groupedRecord['campaign_item_status.title'];
    echo '<br>';
  }
}

$itemsByTitle will be an array where the key is the 'title' field, and the values are an array of all records with that title. So in the top-level array $title should be "Campaign Item 1", and then $groupedRecords should be all of the records returned with the title "Campaign Item 1" which can be looped through to obtain the statuses. (Note this code block wasn't tested, so you may have to modify for your specific page)

One caveat is that array_groupBy() is not the most resource-efficient function, so it may use a lot of CPU or RAM if you are working with a large dataset, or have a high-traffic site. If it causes any performance issues let me know and I can look into other options.

Thanks!

Daniel
Technical Lead
interactivetools.com