-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SearchKit - Add API filter for contacts in groups and smart groups #20507
Conversation
(Standard links)
|
public static function getContactGroupSql(string $fieldAlias, string $operator, $value, Api4SelectQuery $query, int $depth): string { | ||
$tempTable = \CRM_Utils_SQL_TempTable::build(); | ||
$tempTable->createWithColumns('contact_id INT'); | ||
$tableName = $tempTable->getName(); | ||
\CRM_Contact_BAO_GroupContactCache::populateTemporaryTableWithContactsInGroups($value, $tableName); | ||
// SQL optimization - use INNER JOIN if the base table is Contact & this clause is not nested | ||
if ($fieldAlias === '`a`.`id`' && $operator === "IN" && !$depth) { | ||
$query->getQuery()->join($tableName, "INNER JOIN `$tableName` ON $fieldAlias = `$tableName`.contact_id"); | ||
return '1'; | ||
} | ||
// Else use IN or NOT IN (this filter only supports those 2 operators) | ||
else { | ||
return "$fieldAlias $operator (SELECT contact_id FROM `$tableName`)"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eileenmcnaughton this is the meat of it. The rest is to support using a callback like this to supply sql for a field.
Test fails I believe relate here |
Stacktrace |
9155973
to
2841601
Compare
@colemanw is this testable via search kit now? (In which case I should test by trying to create searches on our testing server to give it some performance work to do) |
@eileenmcnaughton yes have fun :) |
Ok - even if this is merged post rc forking (which seems likely based on current test fails) - I'll deploy it with the 5.39 rc to our site so it gets a good kicking |
c097f9b
to
6017503
Compare
Adds 'type' property to API getFields to distinguish regular fields from custom fields, extra fields and filters. Implements `Contact.groups` as a filter, which internally adds a temp-table and incorporates it into the query.
This worked in my tests so far -I'm going to keep testing but I think anything else can be as a follow up as I think the approach won't get us in trouble - ie the surface area of this change is limited to adding the group filter to the api contract. I do think we need a push into documentation next.... |
Overview
Adds a filter-style field to search for contacts in (or not in) one or more groups.
Before
Not possible to search for contacts in groups.
After
Regular groups, smart groups & nested groups all work as SearchKit filters.
Technical Details
Adds some new concepts in APIv4 getFields:
sql_filters
which are php callbacks to generate custom sql.