From f6b4201a720f47143bdd767d49511fc47cb774aa Mon Sep 17 00:00:00 2001
From: JediKev <kevin@enhancesoft.com>
Date: Thu, 14 Dec 2017 12:28:44 -0600
Subject: [PATCH] issue: List & Choice Searching

This addresses issues 3703, 3493, 2625, and others where searching for
List or Choice values in a Custom Field will yield no results. This was
due to us using `FIND_IN_SET()` and `IN()` to search, which will not work
with the way the values are stored in the database. This updates the
search methods to use `REGEXP()` to search for the IDs of the List and
Choice values which will give the correct results whether it's multiple
choice or not.
---
 include/class.dynamic_forms.php | 5 +++--
 include/class.forms.php         | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index ccbbfd220..45f42c0ad 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -1809,11 +1809,12 @@ class SelectionField extends FormField {
 
     function getSearchQ($method, $value, $name=false) {
         $name = $name ?: $this->get('name');
+        $val = '"?'.implode('("|,|$)|"?', array_keys($value)).'("|,|$)';
         switch ($method) {
         case '!includes':
-            return Q::not(array("{$name}__intersect" => array_keys($value)));
+            return Q::not(array("{$name}__regex" => $val));
         case 'includes':
-            return new Q(array("{$name}__intersect" => array_keys($value)));
+            return new Q(array("{$name}__regex" => $val));
         default:
             return parent::getSearchQ($method, $value, $name);
         }
diff --git a/include/class.forms.php b/include/class.forms.php
index c097dd995..e913b7a64 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -1740,11 +1740,12 @@ class ChoiceField extends FormField {
 
     function getSearchQ($method, $value, $name=false) {
         $name = $name ?: $this->get('name');
+        $val = '"?'.implode('("|,|$)|"?', array_keys($value)).'("|,|$)';
         switch ($method) {
         case '!includes':
-            return Q::not(array("{$name}__in" => array_keys($value)));
+            return Q::not(array("{$name}__regex" => $val));
         case 'includes':
-            return new Q(array("{$name}__in" => array_keys($value)));
+            return new Q(array("{$name}__regex" => $val));
         default:
             return parent::getSearchQ($method, $value, $name);
         }
-- 
GitLab