summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDerek Allard <derek.allard@ellislab.com>2007-12-19 00:55:06 +0100
committerDerek Allard <derek.allard@ellislab.com>2007-12-19 00:55:06 +0100
commit80dd702d4c46552a3d1f94c5083c83eeff333b45 (patch)
treeb51f9ef9dfb3b1ff2939caf81d65c3f89947b3dd
parent6ddb5a17ae1a0a75ca75f846dbb7d3a98f1902a3 (diff)
Added where_in() to Active Record.
-rw-r--r--system/database/DB_active_rec.php40
-rw-r--r--user_guide/changelog.html1
-rw-r--r--user_guide/database/active_record.html26
3 files changed, 61 insertions, 6 deletions
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index e8059ab76..c3279e8c3 100644
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -41,6 +41,8 @@ class CI_DB_active_record extends CI_DB_driver {
var $ar_order = FALSE;
var $ar_orderby = array();
var $ar_set = array();
+ var $ar_wherein = array();
+
/**
@@ -240,9 +242,41 @@ class CI_DB_active_record extends CI_DB_driver {
}
return $this;
}
-
-
-
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Where_in
+ *
+ * Generates a WHERE field IN ('item', 'item') SQL query
+ *
+ * @access public
+ * @param string The field to search
+ * @param array The values searched on
+ * @param string
+ * @return object
+ */
+ function where_in($key = NULL, $values = NULL, $type = 'and')
+ {
+ if ($key === NULL || !is_array($values))
+ {
+ return;
+ }
+
+ $type = (strtolower($type) == 'or') ? ' OR ' : ' AND ';
+
+ foreach ($values as $value)
+ {
+ $this->ar_wherein[] = $this->escape($value);
+ }
+
+ $prefix = (count($this->ar_where) == 0) ? '' : $type;
+
+ $this->ar_where[] = $prefix.$key. " IN (" . implode(", ", $this->ar_wherein) . ") ";
+
+ return $this;
+ }
+
// --------------------------------------------------------------------
/**
diff --git a/user_guide/changelog.html b/user_guide/changelog.html
index 612c4db62..8853969c4 100644
--- a/user_guide/changelog.html
+++ b/user_guide/changelog.html
@@ -68,6 +68,7 @@ Change Log
<li>Javascript Calendar plugin now uses the months and days from the calendar language file, instead of hard-coded values, internationalizing it.</li>
<li>Removed &quot;rand()&quot; as a listed option from orderby in the <a href="./database/active_record.html">Active Record</a>, as it was MySQL only.</li>
<li>Added 'random' as an <kbd>order_by()</kbd> option in <a href="./database/active_record.html">Active Record</a>.</li>
+ <li>Added <kbd>where_in()</kbd> to <a href="./database/active_record.html">Active Record</a>.</li>
<li>Added titles to all user manual pages.</li>
<li>Added a check for NULL fields in the MySQL database backup utility.</li>
<li>Documented the <kbd>timezones()</kbd> function in the <a href="./helpers/date_helper.html">Date Helper</a>.</li>
diff --git a/user_guide/database/active_record.html b/user_guide/database/active_record.html
index 739d56a15..aafef33b5 100644
--- a/user_guide/database/active_record.html
+++ b/user_guide/database/active_record.html
@@ -263,11 +263,31 @@ $this->db->where('name !=', $name);<br />
$this->db->or_where('id >', $id);
<br />
<br />// Produces: WHERE name != 'Joe' OR id > 50</code>
-
-
-
<p class="important">Note: or_where() was formerly known as orwhere(), which has been deprecated.</p>
+
+
+<h2>$this->db->where_in();</h2>
+<p>This function is used to write WHERE clauses that contain the IN keyword.</p>
+
+<p><code>
+ $names = array('frank', 'Todd', 'James');<br />
+ $this->db->where_in('username', $names);<br />
+ // Produces: AND WHERE username IN ('frank', 'Todd', 'James')</code></p>
+<p>An optional third parameter can be used to specify if the WHERE statement should be separated with &quot;OR&quot; or &quot;AND&quot; in the event of multiple WHERE calls. The default is &quot;AND&quot;.</p>
+<p><code>$names = array('frank', 'Todd', 'James');<br />
+ <br />
+ $this-&gt;db-&gt;where('usergroup', '5')<br />
+$this-&gt;db-&gt;where_in('username', $names);<br />
+// Produces: WHERE usergroup = '5' AND WHERE username IN ('frank', 'Todd', 'James')<br />
+<br />
+$this-&gt;db-&gt;where('usergroup', '5')<br />
+$this-&gt;db-&gt;where_in('username', $names, 'and');<br />
+// Produces: WHERE usergroup = '5' AND WHERE username IN ('frank', 'Todd', 'James')<br />
+<br />
+$this-&gt;db-&gt;where('usergroup', '5')<br />
+$this-&gt;db-&gt;where_in('username', $names, 'or');<br />
+// Produces: WHERE usergroup = '5' OR WHERE username IN ('frank', 'Todd', 'James')</code></p>
<h2>$this->db->like();</h2>
<p>This function enables you to generate <strong>LIKE</strong> clauses, useful for doing searches.</p>