Skip to content
Snippets Groups Projects
Commit 4775ec6f authored by colemanw's avatar colemanw
Browse files

SearchKit - Fix broken group-by caused by non-aggregated id column

Before: The `id` column was being unconditionally added, breaking FULL_GROUP_BY sql
After: Let the Api4SelectQuery take care of it for DAO entities
parent e8470bff
Branches
Tags
No related merge requests found
......@@ -804,7 +804,10 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction {
}, $apiParams['select']);
$additions = [];
// Add primary key field if actions are enabled
if (!empty($this->display['settings']['actions']) || !empty($this->display['settings']['draggable'])) {
// (only needed for non-dao entities, as Api4SelectQuery will auto-add the id)
if (!in_array('DAOEntity', CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'type')) &&
(!empty($this->display['settings']['actions']) || !empty($this->display['settings']['draggable']))
) {
$additions = CoreUtil::getInfoItem($this->savedSearch['api_entity'], 'primary_key');
}
// Add draggable column (typically "weight")
......
......@@ -2,6 +2,7 @@
namespace api\v4\SearchDisplay;
use Civi\API\Exception\UnauthorizedException;
use Civi\Api4\Activity;
use Civi\Api4\Contact;
use Civi\Api4\ContactType;
use Civi\Api4\Email;
......@@ -666,6 +667,46 @@ class SearchRunTest extends \PHPUnit\Framework\TestCase implements HeadlessInter
}
}
public function testRunWithGroupBy() {
Activity::delete(FALSE)
->addWhere('activity_type_id:name', 'IN', ['Meeting', 'Phone Call'])
->execute();
$cid = Contact::create(FALSE)
->execute()->first()['id'];
$sampleData = [
['subject' => 'abc', 'activity_type_id:name' => 'Meeting', 'source_contact_id' => $cid],
['subject' => 'def', 'activity_type_id:name' => 'Meeting', 'source_contact_id' => $cid],
['subject' => 'xyz', 'activity_type_id:name' => 'Phone Call', 'source_contact_id' => $cid],
];
$aids = Activity::save(FALSE)
->setRecords($sampleData)
->execute()->column('id');
$params = [
'checkPermissions' => FALSE,
'return' => 'page:1',
'savedSearch' => [
'api_entity' => 'Activity',
'api_params' => [
'version' => 4,
'select' => [
"activity_type_id:label",
"GROUP_CONCAT(DISTINCT subject) AS GROUP_CONCAT_subject",
],
'groupBy' => ['activity_type_id'],
'orderBy' => ['activity_type_id:label'],
'where' => [],
],
],
];
$result = civicrm_api4('SearchDisplay', 'run', $params);
$this->assertEquals(['abc', 'def'], $result[0]['data']['GROUP_CONCAT_subject']);
$this->assertEquals(['xyz'], $result[1]['data']['GROUP_CONCAT_subject']);
}
/**
* Test conditional styles
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment