From 218e2bcfd4c9c1ebb2454efc1179d047f40f5b34 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Mon, 17 Dec 2007 21:18:14 +0000 Subject: Added a third parameter to Active Record's like() clause to control where the wildcard goes. Deprecated from Active Record; getwhere() for get_where(); groupby() for group_by(); orderby() for order_by; orwhere() for or_where(); and orlike() for or_like(). --- system/database/DB_active_rec.php | 97 +++++- system/scaffolding/Scaffolding.php | 2 +- user_guide/changelog.html | 2 + user_guide/database/active_record.html | 619 ++++++++++++++++++++++++++++++++- 4 files changed, 704 insertions(+), 16 deletions(-) diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php index 9e30a9fa5..c6e6039e0 100644 --- a/system/database/DB_active_rec.php +++ b/system/database/DB_active_rec.php @@ -185,10 +185,22 @@ class CI_DB_active_record extends CI_DB_driver { * @param mixed * @return object */ - function orwhere($key, $value = NULL) + function or_where($key, $value = NULL) { return $this->_where($key, $value, 'OR '); } + + // -------------------------------------------------------------------- + + /** + * orwhere() is an alias of or_where() + * this function is here for backwards compatibility, as + * orwhere() has been deprecated + */ + function orwhere($key, $value = NULL) + { + return $this->or_where($key, $value); + } // -------------------------------------------------------------------- @@ -244,9 +256,9 @@ class CI_DB_active_record extends CI_DB_driver { * @param mixed * @return object */ - function like($field, $match = '') + function like($field, $match = '', $side = 'both') { - return $this->_like($field, $match, 'AND '); + return $this->_like($field, $match, 'AND ', $side); } // -------------------------------------------------------------------- @@ -262,9 +274,21 @@ class CI_DB_active_record extends CI_DB_driver { * @param mixed * @return object */ - function orlike($field, $match = '') + function or_like($field, $match = '', $side = 'both') { - return $this->_like($field, $match, 'OR '); + return $this->_like($field, $match, 'OR ', $side); + } + + // -------------------------------------------------------------------- + + /** + * orlike() is an alias of or_like() + * this function is here for backwards compatibility, as + * orlike() has been deprecated + */ + function orlike($field, $match = '', $side = 'both') + { + return $this->orlike($field, $match, $side); } // -------------------------------------------------------------------- @@ -280,7 +304,7 @@ class CI_DB_active_record extends CI_DB_driver { * @param string * @return object */ - function _like($field, $match = '', $type = 'AND ') + function _like($field, $match = '', $type = 'AND ', $side = 'both') { if ( ! is_array($field)) { @@ -292,8 +316,19 @@ class CI_DB_active_record extends CI_DB_driver { $prefix = (count($this->ar_like) == 0) ? '' : $type; $v = $this->escape_str($v); - - $this->ar_like[] = $prefix." $k LIKE '%{$v}%'"; + + if ($side == 'before') + { + $this->ar_like[] = $prefix." $k LIKE '%{$v}'"; + } + elseif ($side == 'after') + { + $this->ar_like[] = $prefix." $k LIKE '{$v}%'"; + } + else + { + $this->ar_like[] = $prefix." $k LIKE '%{$v}%'"; + } } return $this; } @@ -307,7 +342,7 @@ class CI_DB_active_record extends CI_DB_driver { * @param string * @return object */ - function groupby($by) + function group_by($by) { if (is_string($by)) { @@ -323,7 +358,19 @@ class CI_DB_active_record extends CI_DB_driver { } return $this; } - + + // -------------------------------------------------------------------- + + /** + * groupby() is an alias of group_by() + * this function is here for backwards compatibility, as + * groupby() has been deprecated + */ + function groupby($by) + { + return $this->group_by($by); + } + // -------------------------------------------------------------------- /** @@ -401,7 +448,7 @@ class CI_DB_active_record extends CI_DB_driver { * @param string direction: asc or desc * @return object */ - function orderby($orderby, $direction = '') + function order_by($orderby, $direction = '') { if (trim($direction) != '') { @@ -411,7 +458,17 @@ class CI_DB_active_record extends CI_DB_driver { $this->ar_orderby[] = $orderby.$direction; return $this; } - + // -------------------------------------------------------------------- + + /** + * orderby() is an alias of order_by() + * this function is here for backwards compatibility, as + * orderby() has been deprecated + */ + function orderby($orderby, $direction = '') + { + return $this->order_by($orderby, $direction); + } // -------------------------------------------------------------------- /** @@ -509,7 +566,7 @@ class CI_DB_active_record extends CI_DB_driver { // -------------------------------------------------------------------- /** - * GetWhere + * Get_Where * * Allows the where clause, limit and offset to be added directly * @@ -519,7 +576,7 @@ class CI_DB_active_record extends CI_DB_driver { * @param string the offset clause * @return object */ - function getwhere($table = '', $where = null, $limit = null, $offset = null) + function get_where($table = '', $where = null, $limit = null, $offset = null) { if ($table != '') { @@ -542,6 +599,18 @@ class CI_DB_active_record extends CI_DB_driver { $this->_reset_select(); return $result; } + + // -------------------------------------------------------------------- + + /** + * getwhere() is an alias of get_where() + * this function is here for backwards compatibility, as + * getwhere() has been deprecated + */ + function getwhere($table = '', $where = null, $limit = null, $offset = null) + { + return $this->get_where($table, $where, $limit, $offset); + } // -------------------------------------------------------------------- diff --git a/system/scaffolding/Scaffolding.php b/system/scaffolding/Scaffolding.php index 70c6156ad..8ddb1af07 100644 --- a/system/scaffolding/Scaffolding.php +++ b/system/scaffolding/Scaffolding.php @@ -204,7 +204,7 @@ class Scaffolding { $primary = $this->CI->db->primary($this->current_table); // Run the query - $query = $this->CI->db->getwhere($this->current_table, array($primary => $id)); + $query = $this->CI->db->get_where($this->current_table, array($primary => $id)); $data = array( 'title' => ( ! isset($this->lang['scaff_edit'])) ? 'Edit Data' : $this->lang['scaff_edit'], diff --git a/user_guide/changelog.html b/user_guide/changelog.html index 9c2399263..a47d3a019 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -78,6 +78,7 @@ Change Log
  • Added quotes_to_entities() function to string helper.
  • Added form_reset() function to form helper.
  • Added a language entry for valid_ip validation error.
  • +
  • Added a third parameter to Active Record's like() clause to control where the wildcard goes.
  • Moved the safe mode and auth checks for the Email library into the constructor.
  • Fixed a bug in highlight_pharse() that caused an error with slashes.
  • Fixed a bug in word_wrap() of the Text Helper that incorrectly referenced an object.
  • @@ -101,6 +102,7 @@ Change Log
  • Fixed a typo in the DocBlock comment for unset_userdata() in Session.
  • Fixed a typo in the table library docs.
  • Moved part of the userguide menu javascript to an external file.
  • +
  • Deprecated from Active Record; getwhere() for get_where(); groupby() for group_by(); orderby() for order_by; orwhere() for or_where(); and orlike() for or_like().
  • diff --git a/user_guide/database/active_record.html b/user_guide/database/active_record.html index ee7b245d6..1c4783ab9 100644 --- a/user_guide/database/active_record.html +++ b/user_guide/database/active_record.html @@ -1 +1,618 @@ - CodeIgniter User Guide : Active Record

    CodeIgniter User Guide Version 1.5.4


    Active Record Class

    CodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

    Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system.

    Note: If you intend to write your own queries you can disable this class in your database config file, allowing the core database library and adapter to utilize fewer resources.


     

    Selecting Data

    The following functions allow you to build SQL SELECT statements.

    Note: If you are using PHP 5 you can use method chaining for more compact syntax. This is described at the end of the page.

    $this->db->get();

    Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:

    $query = $this->db->get('mytable');

    // Produces: SELECT * FROM mytable

    The second and third parameters enable you do set a limit and offset clause:

    $query = $this->db->get('mytable', 10, 20);

    // Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)

    You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:

    $query = $this->db->get('mytable');

    foreach ($query->result() as $row)
    {
        echo $row->title;
    }

    Please visit the result functions page for a full discussion regarding result generation.

    $this->db->getwhere();

    Identical to the above function except that it permits you to add a "where" clause in the second parameter, instead of using the db->where() function:

    $query = $this->db->getwhere('mytable', array('id' => $id), $limit, $offset);

    Please read the about the where function below for more information.

    $this->db->select();

    Permits you to write the SELECT portion of your query:

    $this->db->select('title, content, date');

    $query = $this->db->get('mytable');

    // Produces: SELECT title, content, date FROM mytable

    Note: If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *

    $this->db->from();

    Permits you to write the FROM portion of your query:

    $this->db->select('title, content, date');
    $this->db->from('mytable');

    $query = $this->db->get();

    // Produces: SELECT title, content, date FROM mytable

    Note: As shown earlier, the FROM portion of your query can be specified in the $this->db->get() function, so use whichever method you prefer.

    $this->db->join();

    Permits you to write the JOIN portion of your query:

    $this->db->select('*');
    $this->db->from('blogs');
    $this->db->join('comments', 'comments.id = blogs.id');

    $query = $this->db->get();

    // Produces:
    // SELECT * FROM blogs
    // JOIN comments ON comments.id = blogs.id

    Multiple function calls can be made if you need several joins in one query.

    If you need something other than a natural JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.

    $this->db->join('comments', 'comments.id = blogs.id', 'left');

    // Produces: LEFT JOIN comments ON comments.id = blogs.id

    $this->db->where();

    This function enables you to set WHERE clauses using one of four methods:

    Note: All values passed to this function are escaped automatically, producing safer queries.

    1. Simple key/value method: $this->db->where('name', $name);

      // Produces: WHERE name = 'Joe'

      Notice that the equal sign is added for you.

      If you use multiple function calls they will be chained together with AND between them:

      $this->db->where('name', $name);
      $this->db->where('title', $title);
      $this->db->where('status', $status);

      // WHERE = 'Joe' AND title = 'boss' AND status = 'active'
    2. Custom key/value method:

      You can include an operator in the first parameter in order to control the comparison:

      $this->db->where('name !=', $name);
      $this->db->where('id <', $id);

      // Produces: WHERE name != 'Joe' AND id < 45
    3. Associative array method: $array = array('name' => $name, 'title' => $title, 'status' => $status);

      $this->db->where($array);

      // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

      You can include your own operators using this method as well:

      $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);

      $this->db->where($array);
    4. Custom string:

      You can write your own clauses manually:

      $where = "name='Joe' AND status='boss' OR status='active'";

      $this->db->where($where);

    $this->db->orwhere();

    This function is identical to the one above, except that multiple instances are joined by OR:

    $this->db->where('name !=', $name);
    $this->db->orwhere('id >', $id);

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

    $this->db->like();

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

    Note: All values passed to this function are escaped automatically.

    1. Simple key/value method: $this->db->like('title', $match);

      // Produces: WHERE title LIKE '%match%'

      If you use multiple function calls they will be chained together with AND between them:

      $this->db->like('title', $match);
      $this->db->like('body', $match);

      // WHERE title LIKE '%match%' AND body LIKE '%match%'
    2. Associative array method: $array = array('title' => $match, 'page1' => $match, 'page2' => $match);

      $this->db->like($array);

      // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'

    $this->db->orlike();

    This function is identical to the one above, except that multiple instances are joined by OR:

    $this->db->like('title', $match);
    $this->db->orlike('body', $match);

    // WHERE title LIKE '%match%' OR body LIKE '%match%'

    $this->db->groupby();

    Permits you to write the GROUP BY portion of your query:

    $this->db->groupby("title");

    // Produces: GROUP BY title

    You can also pass an array of multiple values as well:

    $this->db->groupby(array("title", "date"));

    // Produces: GROUP BY title, date

    $this->db->having();

    Permits you to write the HAVING portion of your query:

    $this->db->having('user_id = 45');

    // Produces: HAVING 'user_id = 45'

    You can also pass an array of multiple values as well:

    $this->db->having(array('title =' => 'My Title', 'id <' => $id));

    // Produces: HAVING title = 'My Title', 'id < 45'

    $this->db->orderby();

    Lets you set an ORDER BY clause. The first parameter contains the name of the column you would like to order by. The second parameter lets you set the direction of the result. Options are asc or desc

    $this->db->orderby("title", "desc");

    // Produces: ORDER BY title DESC

    You can also pass your own string in the first parameter:

    $this->db->orderby('title desc, name asc');

    // Produces: ORDER BY title DESC, name ASC

    Or multiple function calls can be made if you need multiple fields.

    $this->db->orderby("title", "desc");
    $this->db->orderby("name", "asc");

    // Produces: ORDER BY title DESC, name ASC

    $this->db->limit();

    Lets you limit the number of rows you would like returned by the query:

    $this->db->limit(10);

    // Produces: LIMIT 10

    The second parameter lets you set a result offset.

    $this->db->limit(10, 20);

    // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)

    $this->db->count_all();

    Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:

    echo $this->db->count_all('my_table');

    // Produces an integer, like 25
     

    Inserting Data

    $this->db->insert();

    Generates an insert string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:

    $data = array(
                   'title' => 'My title' ,
                   'name' => 'My Name' ,
                   'date' => 'My date'
                );

    $this->db->insert('mytable', $data);

    // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

    The first parameter will contain the table name, the second is an associative array of values.

    Here is an example using an object:

    /*
        class Myclass {
            var $title = 'My Title';
            var $content = 'My Content';
            var $date = 'My Date';
        }
    */

    $object = new Myclass;

    $this->db->insert('mytable', $object);

    // Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')

    The first parameter will contain the table name, the second is an associative array of values.

    Note: All values are escaped automatically producing safer queries.

    $this->db->set();

    This function enables you to set values for inserts or updates.

    It can be used instead of passing a data array directly to the insert or update functions:

    $this->db->set('name', $name);
    $this->db->insert('mytable');

    // Produces: INSERT INTO mytable (name) VALUES ('{$name}')

    If you use multiple function called they will be assembled properly based on whether you are doing an insert or an update:

    $this->db->set('name', $name);
    $this->db->set('title', $title);
    $this->db->set('status', $status);
    $this->db->insert('mytable');

    You can also pass an associative array to this function:

    $array = array('name' => $name, 'title' => $title, 'status' => $status);

    $this->db->set($array);
    $this->db->insert('mytable');

    Or an object:

    /*
        class Myclass {
            var $title = 'My Title';
            var $content = 'My Content';
            var $date = 'My Date';
        }
    */

    $object = new Myclass;

    $this->db->set($object);
    $this->db->insert('mytable');
     

    Updating Data

    $this->db->update();

    Generates an update string and runs the query based on the data you supply. You can pass an array or an object to the function. Here is an example using an array:

    $data = array(
                   'title' => $title,
                   'name' => $name,
                   'date' => $date
                );

    $this->db->where('id', $id);
    $this->db->update('mytable', $data);

    // Produces:
    // UPDATE mytable
    // SET title = '{$title}', name = '{$name}', date = '{$date}'
    // WHERE id = $id

    Or you can supply an object:

    /*
        class Myclass {
            var $title = 'My Title';
            var $content = 'My Content';
            var $date = 'My Date';
        }
    */

    $object = new Myclass;

    $this->db->where('id', $id);
    $this->db->update('mytable', $object);

    // Produces:
    // UPDATE mytable
    // SET title = '{$title}', name = '{$name}', date = '{$date}'
    // WHERE id = $id

    Note: All values are escaped automatically producing safer queries.

    You'll notice the use of the $this->db->where() function, enabling you to set the WHERE clause. You can optionally pass this information directly into the update function as a string:

    $this->db->update('mytable', $data, "id = 4");

    Or as an array:

    $this->db->update('mytable', $data, array('id' => $id));

    You may also use the $this->db->set() function described above when performing updates.

     

    Deleting Data

    $this->db->delete();

    Generates a delete SQL string and runs the query.

    $this->db->delete('mytable', array('id' => $id));

    // Produces:
    // DELETE FROM mytable
    // WHERE id = $id

    The first parameter is the table name, the second is the where clause. You can also use the where() or orwhere() functions instead of passing the data to the second parameter of the function:

    $this->db->where('id', $id);
    $this->db->delete('mytable');

    // Produces:
    // DELETE FROM mytable
    // WHERE id = $id

    Note: All values are escaped automatically producing safer queries.

     

    Method Chaining

    Method chaining allows you to simplify your syntax by connecting multiple functions. Consider this example:

    $this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);

    $query = $this->db->get();

    Note: Method chaining only works with PHP 5.

    \ No newline at end of file + + + + +CodeIgniter User Guide : Active Record + + + + + + + + + + + + + + + + + + + + + + +
    + + + + + +

    CodeIgniter User Guide Version 1.5.4

    +
    + + + + + + + + + +
    + + + +
    + + + +
    + + +

    Active Record Class

    + + +

    CodeIgniter uses a modified version of the Active Record Database Pattern. +This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. +In some cases only one or two lines of code are necessary to perform a database action. +CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

    + +

    Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax +is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system.

    + +

    Note: If you intend to write your own queries you can disable this class in your database config file, allowing the core database library and adapter to utilize fewer resources.

    + +
    + + + + + + +  +

    Selecting Data

    + +

    The following functions allow you to build SQL SELECT statements.

    + +

    Note: If you are using PHP 5 you can use method chaining for more compact syntax. This is described at the end of the page.

    + + +

    $this->db->get();

    + +

    Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:

    + +$query = $this->db->get('mytable');
    +
    +// Produces: SELECT * FROM mytable
    + +

    The second and third parameters enable you do set a limit and offset clause:

    + +$query = $this->db->get('mytable', 10, 20);
    +
    +// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
    + + + +

    You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:

    + +$query = $this->db->get('mytable');
    +
    +foreach ($query->result() as $row)
    +{
    +    echo $row->title;
    +}
    + +

    Please visit the result functions page for a full discussion regarding result generation.

    + + +

    $this->db->get_where();

    + +

    Identical to the above function except that it permits you to add a "where" clause in the second parameter, +instead of using the db->where() function:

    + +$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset); + +

    Please read the about the where function below for more information.

    +

    Note: get_where() was formerly known as getwhere(), which has been deprecated

    +

    $this->db->select();

    + +

    Permits you to write the SELECT portion of your query:

    + + +$this->db->select('title, content, date');
    +
    +$query = $this->db->get('mytable');
    +
    +// Produces: SELECT title, content, date FROM mytable
    + +

    Note: If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *

    + + +

    $this->db->from();

    + +

    Permits you to write the FROM portion of your query:

    + + +$this->db->select('title, content, date');
    +$this->db->from('mytable');
    +
    +$query = $this->db->get();
    +
    +// Produces: SELECT title, content, date FROM mytable
    + +

    Note: As shown earlier, the FROM portion of your query can be specified in the $this->db->get() function, so use whichever method +you prefer.

    + +

    $this->db->join();

    + +

    Permits you to write the JOIN portion of your query:

    + + +$this->db->select('*');
    +$this->db->from('blogs');
    +$this->db->join('comments', 'comments.id = blogs.id');
    +
    +$query = $this->db->get();
    +
    +// Produces:
    +// SELECT * FROM blogs
    +// JOIN comments ON comments.id = blogs.id
    +
    + +

    Multiple function calls can be made if you need several joins in one query.

    + +

    If you need something other than a natural JOIN you can specify it via the third parameter of the function. +Options are: left, right, outer, inner, left outer, and right outer.

    + + +$this->db->join('comments', 'comments.id = blogs.id', 'left');
    +
    +// Produces: LEFT JOIN comments ON comments.id = blogs.id
    + + + + + +

    $this->db->where();

    +

    This function enables you to set WHERE clauses using one of four methods:

    + +

    Note: All values passed to this function are escaped automatically, producing safer queries.

    + +
      +
    1. Simple key/value method: + + $this->db->where('name', $name); +

      // Produces: WHERE name = 'Joe' +
      + +

      Notice that the equal sign is added for you.

      + +

      If you use multiple function calls they will be chained together with AND between them:

      + + $this->db->where('name', $name);
      + $this->db->where('title', $title);
      + $this->db->where('status', $status); +

      // WHERE = 'Joe' AND title = 'boss' AND status = 'active' +
      + + +
    2. + +
    3. Custom key/value method: + +

      You can include an operator in the first parameter in order to control the comparison:

      + + $this->db->where('name !=', $name);
      + $this->db->where('id <', $id); +

      // Produces: WHERE name != 'Joe' AND id < 45 +
      + + + +
    4. +
    5. Associative array method: + + + + $array = array('name' => $name, 'title' => $title, 'status' => $status);

      + + $this->db->where($array); +

      // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' +
      + +

      You can include your own operators using this method as well:

      + + + $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);

      + + $this->db->where($array);
      + +
    6. +
    7. Custom string: + +

      You can write your own clauses manually:

      + + + $where = "name='Joe' AND status='boss' OR status='active'";

      + $this->db->where($where);
      + +
    8. +
    + + +

    $this->db->or_where();

    +

    This function is identical to the one above, except that multiple instances are joined by OR:

    + + +$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->like();

    +

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

    + +

    Note: All values passed to this function are escaped automatically.

    + + +
      +
    1. Simple key/value method: + + $this->db->like('title', 'match'); +

      // Produces: WHERE title LIKE '%match%'
      + +

      If you use multiple function calls they will be chained together with AND between them:

      + + $this->db->like('title', 'match');
      + $this->db->like('body', 'match'); +

      + // WHERE title LIKE '%match%' AND body LIKE '%match%
      + If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are 'before', 'after' and 'both' (which is the default). + $this->db->like('title', 'match', 'before'); +
      + // Produces: WHERE title LIKE '%match'
      +
      + $this->db->like('title', 'match', 'after');
      +// Produces: WHERE title LIKE 'match%'
      +
      + $this->db->like('title', 'match', 'both');
      +// Produces: WHERE title LIKE '%match%'
    2. + +
    3. Associative array method: + + + $array = array('title' => $match, 'page1' => $match, 'page2' => $match);

      + + $this->db->like($array); +

      // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'
    4. +
    + + +

    $this->db->or_like();

    +

    This function is identical to the one above, except that multiple instances are joined by OR:

    + + +$this->db->like('title', $match);
    +$this->db->or_like('body', $match); +
    +
    // WHERE title LIKE '%match%' OR body LIKE '%match%'
    + + + + +

    Note: or_like() was formerly known as orlike(), which has been deprecated.

    +

    $this->db->group_by();

    +

    Permits you to write the GROUP BY portion of your query:

    + +$this->db->group_by("title"); +

    // Produces: GROUP BY title +
    + +

    You can also pass an array of multiple values as well:

    + +$this->db->group_by(array("title", "date")); +
    +
    // Produces: GROUP BY title, date
    + + +

    Note: group_by() was formerly known as groupby(), which has been deprecated.

    +

    $this->db->having();

    +

    Permits you to write the HAVING portion of your query:

    + +$this->db->having('user_id = 45'); +

    // Produces: HAVING 'user_id = 45' +
    + +

    You can also pass an array of multiple values as well:

    + + +$this->db->having(array('title =' => 'My Title', 'id <' => $id)); +

    // Produces: HAVING title = 'My Title', 'id < 45' +
    + + + +

    $this->db->order_by();

    +

    Lets you set an ORDER BY clause. The first parameter contains the name of the column you would like to order by. +The second parameter lets you set the direction of the result. Options are asc or desc

    + +$this->db->order_by("title", "desc"); +
    +
    // Produces: ORDER BY title DESC +
    + +

    You can also pass your own string in the first parameter:

    + +$this->db->order_by('title desc, name asc'); +
    +
    // Produces: ORDER BY title DESC, name ASC +
    + +

    Or multiple function calls can be made if you need multiple fields.

    + +

    $this->db->order_by("title", "desc");
    + $this->db->order_by("name", "asc");
    +
    + // Produces: ORDER BY title DESC, name ASC +

    +

    Note: order_by() was formerly known as orderby(), which has been deprecated.

    +

    $this->db->limit();

    +

    Lets you limit the number of rows you would like returned by the query:

    + + +$this->db->limit(10);
    +
    +// Produces: LIMIT 10
    + + +

    The second parameter lets you set a result offset.

    + + +$this->db->limit(10, 20);
    +
    +// Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
    + + +

    $this->db->count_all();

    + +

    Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:

    + +echo $this->db->count_all('my_table');
    +
    +// Produces an integer, like 25
    + + + + +  +

    Inserting Data

    + +

    $this->db->insert();

    +

    Generates an insert string based on the data you supply, and runs the query. You can either pass an +array or an object to the function. Here is an example using an array:

    + + +$data = array(
    +               'title' => 'My title' ,
    +               'name' => 'My Name' ,
    +               'date' => 'My date'
    +            );
    +
    +$this->db->insert('mytable', $data); +

    +// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
    + +

    The first parameter will contain the table name, the second is an associative array of values.

    + +

    Here is an example using an object:

    + + +/*
    +    class Myclass {
    +        var $title = 'My Title';
    +        var $content = 'My Content';
    +        var $date = 'My Date';
    +    }
    +*/
    +
    +$object = new Myclass;
    +
    +$this->db->insert('mytable', $object); +

    +// Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')
    + +

    The first parameter will contain the table name, the second is an associative array of values.

    + +

    Note: All values are escaped automatically producing safer queries.

    + + + + +

    $this->db->set();

    +

    This function enables you to set values for inserts or updates.

    + +

    It can be used instead of passing a data array directly to the insert or update functions:

    + +$this->db->set('name', $name); +
    +$this->db->insert('mytable'); +

    +// Produces: INSERT INTO mytable (name) VALUES ('{$name}')
    + +

    If you use multiple function called they will be assembled properly based on whether you are doing an insert or an update:

    + +$this->db->set('name', $name);
    +$this->db->set('title', $title);
    +$this->db->set('status', $status);
    +$this->db->insert('mytable'); +
    + +

    You can also pass an associative array to this function:

    + + +$array = array('name' => $name, 'title' => $title, 'status' => $status);

    + +$this->db->set($array);
    +$this->db->insert('mytable'); +
    + +

    Or an object:

    + + + +/*
    +    class Myclass {
    +        var $title = 'My Title';
    +        var $content = 'My Content';
    +        var $date = 'My Date';
    +    }
    +*/
    +
    +$object = new Myclass;
    +
    +$this->db->set($object);
    +$this->db->insert('mytable'); +
    + + + +  +

    Updating Data

    + +

    $this->db->update();

    +

    Generates an update string and runs the query based on the data you supply. You can pass an +array or an object to the function. Here is an example using +an array:

    + + +$data = array(
    +               'title' => $title,
    +               'name' => $name,
    +               'date' => $date
    +            );
    +
    +$this->db->where('id', $id);
    +$this->db->update('mytable', $data); +

    +// Produces:
    +// UPDATE mytable
    +// SET title = '{$title}', name = '{$name}', date = '{$date}'
    +// WHERE id = $id
    + +

    Or you can supply an object:

    + + +/*
    +    class Myclass {
    +        var $title = 'My Title';
    +        var $content = 'My Content';
    +        var $date = 'My Date';
    +    }
    +*/
    +
    +$object = new Myclass;
    +
    +$this->db->where('id', $id);
    +$this->db->update('mytable', $object); +
    +
    +// Produces:
    +// UPDATE mytable
    +// SET title = '{$title}', name = '{$name}', date = '{$date}'
    +// WHERE id = $id
    + + + +

    Note: All values are escaped automatically producing safer queries.

    + +

    You'll notice the use of the $this->db->where() function, enabling you to set the WHERE clause. +You can optionally pass this information directly into the update function as a string:

    + +$this->db->update('mytable', $data, "id = 4"); + +

    Or as an array:

    + +$this->db->update('mytable', $data, array('id' => $id)); + +

    You may also use the $this->db->set() function described above when performing updates.

    + + +  +

    Deleting Data

    + + + +

    $this->db->delete();

    +

    Generates a delete SQL string and runs the query.

    + + +$this->db->delete('mytable', array('id' => $id)); +

    +// Produces:
    +// DELETE FROM mytable
    +// WHERE id = $id
    + +

    The first parameter is the table name, the second is the where clause. You can also use the where() or or_where() functions instead of passing +the data to the second parameter of the function:

    + + +$this->db->where('id', $id);
    +$this->db->delete('mytable'); +

    +// Produces:
    +// DELETE FROM mytable
    +// WHERE id = $id
    + +

    Note: All values are escaped automatically producing safer queries.

    + + +  +

    Method Chaining

    + +

    Method chaining allows you to simplify your syntax by connecting multiple functions. Consider this example:

    + + +$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
    +
    +$query = $this->db->get();
    + +

    Note: Method chaining only works with PHP 5.

    + + + +
    + + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b