XF 2.1 Finder code - Exclude by secondary_group_ids

AndyB

Well-known member
Currently I have the following code which will find users which do not have a secondary_group_ids. However I would like to update this finder code so I can exclude by secondary_group_ids. I will add to the Options page of the add-on a field which allows selecting user groups. I'm using the "XF\Option\UserGroup::renderSelectMultiple" , a variable called "excludeUserGroups" will be used. How would I modify the code below?

Thank you.

PHP:
$finder = \XF::finder('XF:User');
$users = $finder
    ->where('secondary_group_ids', '=', '')
    ->fetch();
 
Code:
$finder = \XF::finder('XF:User');
$users = $finder
    ->where('secondary_group_ids', '<>', '')
    ->fetch();
 
Hi Lukas,

Thank you for your reply.

I should be more clear. The secondary_group_ids could contain some IDs which are not excluded. The finder code needs to check if any value in the "excludeUserGroups" array is contained the secondary_group_ids.
 
PHP:
$columnName = $finder->columnSqlName('secondary_group_ids');
$parts = [];
foreach ($userGroupIds AS $part)
{
    $parts[] = 'FIND_IN_SET(' . $finder->quote($part) . ', '. $columnName . ') = 0';
}

if ($parts)
{
    $finder->whereSql(implode(' OR ', $parts));
}
 
Top Bottom