Skip to content

Instantly share code, notes, and snippets.

@narfbg
Created June 5, 2012 21:19
Show Gist options
  • Save narfbg/2878052 to your computer and use it in GitHub Desktop.
Save narfbg/2878052 to your computer and use it in GitHub Desktop.
CodeIgniter QB join() with multiple conditions
diff --git a/system/database/DB_query_builder.php b/system/database/DB_query_builder.php
index 7a0ea0c..cefaa77 100644
--- a/system/database/DB_query_builder.php
+++ b/system/database/DB_query_builder.php
@@ -343,8 +343,26 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
// in the protect_identifiers to know whether to add a table prefix
$this->_track_aliases($table);
- // Strip apart the condition and protect the identifiers
- if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $cond, $match))
+ // Split multiple conditions
+ if (preg_match_all('/\sAND\s|\sOR\s/i', $cond, $m, PREG_SET_ORDER | PREG_OFFSET_CAPTURE))
+ {
+ $newcond = '';
+ $m[0][] = array('', strlen($cond));
+ for ($i = 0, $c = count($m[0]), $s = 0;
+ $i < $count;
+ $s += $m[0][$i][1] + strlen($m[0][$i][0]), $i++)
+ {
+ $temp = substr($cond, $start, $m[0][$i][1]);
+
+ $newcond .= preg_match('/([\[\w\.]+)([\W\s]+)(.+)/i', $temp, $match)
+ ? $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3])
+ : $temp;
+ }
+
+ $cond = $newcond;
+ }
+ // Split apart the condition and protect the identifiers
+ elseif (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/i', $cond, $match))
{
$cond = $this->protect_identifiers($match[1]).$match[2].$this->protect_identifiers($match[3]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment