I'm using the following code.
$query = \Drupal::entityQuery('user');
$query->condition('uid', [302,330], 'IN')
->condition('status', 1, '=')
->condition('field_membership_status.value', 'paid', '!=')
->condition('roles', ['individual','organization'], 'IN')
->condition('roles', ['organization_member'], 'NOT IN');
$uids = $query->execute();
$users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple(array_values($uids));
foreach ($users as $user) {
$type= "Final membership reminder";
\Drupal::logger('my_module')->notice("email to ".$user->getAccountName());
\Drupal::service('vc_mailer.email.service')->send_email($type, $user);
}
One of the user accounts has the following roles: authenticated, individual, and organization_member. This user account is still returned by the code; it seems like the code ignores the last condition. What is the problem?
condition('uid', [302,330], 'IN')
is just to constrain the query to those members for testing purposes.
The SQL query is the following.
SELECT "base_table"."uid" AS "uid", "base_table"."uid" AS "base_table_uid" FROM "users" "base_table" INNER JOIN "users_field_data" "users_field_data" ON "users_field_data"."uid" = "base_table"."uid" INNER JOIN "user__field_membership_status" "user__field_membership_status" ON "user__field_membership_status"."entity_id" = "base_table"."uid" INNER JOIN "user__roles" "user__roles" ON "user__roles"."entity_id" = "base_table"."uid" WHERE ("users_field_data"."uid" IN ('302', '330')) AND ("users_field_data"."status" = '1') AND ("user__field_membership_status"."field_membership_status_value" != 'paid') AND ("user__roles"."roles_target_id" IN ('individual', 'organization')) AND ("user__roles"."roles_target_id" NOT IN ('organization_member'))
andConditionGroup()
. See drupal.stackexchange.com/questions/226396/…