From 80dd702d4c46552a3d1f94c5083c83eeff333b45 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Tue, 18 Dec 2007 23:55:06 +0000 Subject: Added where_in() to Active Record. --- system/database/DB_active_rec.php | 40 +++++++++++++++++++++++++++++++--- user_guide/changelog.html | 1 + user_guide/database/active_record.html | 26 +++++++++++++++++++--- 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
  • Javascript Calendar plugin now uses the months and days from the calendar language file, instead of hard-coded values, internationalizing it.
  • Removed "rand()" as a listed option from orderby in the Active Record, as it was MySQL only.
  • Added 'random' as an order_by() option in Active Record.
  • +
  • Added where_in() to Active Record.
  • Added titles to all user manual pages.
  • Added a check for NULL fields in the MySQL database backup utility.
  • Documented the timezones() function in the Date Helper.
  • 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);
    $this->db->or_where('id >', $id);

    // Produces: WHERE name != 'Joe' OR id > 50 - - -

    Note: or_where() was formerly known as orwhere(), which has been deprecated.

    + + +

    $this->db->where_in();

    +

    This function is used to write WHERE clauses that contain the IN keyword.

    + +

    + $names = array('frank', 'Todd', 'James');
    + $this->db->where_in('username', $names);
    + // Produces: AND WHERE username IN ('frank', 'Todd', 'James')

    +

    An optional third parameter can be used to specify if the WHERE statement should be separated with "OR" or "AND" in the event of multiple WHERE calls. The default is "AND".

    +

    $names = array('frank', 'Todd', 'James');
    +
    + $this->db->where('usergroup', '5')
    +$this->db->where_in('username', $names);
    +// Produces: WHERE usergroup = '5' AND WHERE username IN ('frank', 'Todd', 'James')
    +
    +$this->db->where('usergroup', '5')
    +$this->db->where_in('username', $names, 'and');
    +// Produces: WHERE usergroup = '5' AND WHERE username IN ('frank', 'Todd', 'James')
    +
    +$this->db->where('usergroup', '5')
    +$this->db->where_in('username', $names, 'or');
    +// Produces: WHERE usergroup = '5' OR WHERE username IN ('frank', 'Todd', 'James')

    $this->db->like();

    This function enables you to generate LIKE clauses, useful for doing searches.

    -- cgit v1.2.3-24-g4f1b