From fa109177ce3cb5f6f881419d34399b44b3ec69b1 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 28 Nov 2014 21:14:04 -0600
Subject: [PATCH] orm: Add set intersection support

---
 include/class.orm.php | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/include/class.orm.php b/include/class.orm.php
index 0146b9d5b..3508d9273 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -1522,6 +1522,7 @@ class MySqlCompiler extends SqlCompiler {
         'like' => '%1$s LIKE %2$s',
         'hasbit' => '%1$s & %2$s != 0',
         'in' => array('self', '__in'),
+        'intersect' => array('self', '__find_in_set'),
     );
 
     // Thanks, http://stackoverflow.com/a/3683868
@@ -1561,6 +1562,19 @@ class MySqlCompiler extends SqlCompiler {
             : sprintf('%s IS NOT NULL', $a);
     }
 
+    function __find_in_set($a, $b) {
+        if (is_array($b)) {
+            $sql = array();
+            foreach (array_map(array($this, 'input'), $b) as $b) {
+                $sql[] = sprintf('FIND_IN_SET(%s, %s)', $b, $a);
+            }
+            $parens = count($sql) > 1;
+            $sql = implode(' OR ', $sql);
+            return $parens ? ('('.$sql.')') : $sql;
+        }
+        return sprintf('FIND_IN_SET(%s, %s)', $b, $a);
+    }
+
     function compileJoin($tip, $model, $alias, $info, $extra=false) {
         $constraints = array();
         $join = ' JOIN ';
-- 
GitLab