Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
narfbg committed Nov 29, 2012
1 parent cc4b003 commit 4173823
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
5 changes: 3 additions & 2 deletions system/database/DB_query_builder.php
Expand Up @@ -903,11 +903,12 @@ protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $n
}

is_bool($escape) OR $escape = $this->_protect_identifiers;
$prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
? $this->_group_get_type('') : $this->_group_get_type($type);

foreach ($field as $k => $v)
{
$prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
? $this->_group_get_type('') : $this->_group_get_type($type);

$v = $this->escape_like_str($v);

if ($side === 'none')
Expand Down
4 changes: 3 additions & 1 deletion tests/codeigniter/core/Output_test.php
Expand Up @@ -2,11 +2,13 @@

class Output_test extends CI_TestCase {

public $output;

public function set_up()
{
$this->ci_set_config('charset', 'UTF-8');
$output = $this->ci_core_class('output');
$this->output = new $output();
$this->output = new $output();
}

// --------------------------------------------------------------------
Expand Down
37 changes: 19 additions & 18 deletions user_guide_src/source/database/query_builder.rst
Expand Up @@ -345,71 +345,72 @@ if appropriate
$this->db->like()
=================

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

.. note:: All values passed to this function are escaped automatically.
.. note:: All values passed to this method are escaped automatically.

#. **Simple key/value method:**

::

$this->db->like('title', 'match'); // Produces: WHERE title LIKE '%match%'
$this->db->like('title', 'match');
// Produces: WHERE `title` LIKE '%match%' ESCAPE '!'

If you use multiple function calls they will be chained together with
If you use multiple method 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%
// WHERE `title` LIKE '%match%' ESCAPE '!' AND `body` LIKE '%match% ESCAPE '!'

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%'
$this->db->like('title', 'match', 'before'); // Produces: WHERE `title` LIKE '%match' ESCAPE '!'
$this->db->like('title', 'match', 'after'); // Produces: WHERE `title` LIKE 'match%' ESCAPE '!'
$this->db->like('title', 'match', 'both'); // Produces: WHERE `title` LIKE '%match%' ESCAPE '!'

#. **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%'
// WHERE `title` LIKE '%match%' ESCAPE '!' AND `page1` LIKE '%match%' ESCAPE '!' AND `page2` LIKE '%match%' ESCAPE '!'


$this->db->or_like()
====================

This function is identical to the one above, except that multiple
This method 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%'
// WHERE `title` LIKE '%match%' ESCAPE '!' OR `body` LIKE '%match%' ESCAPE '!'

.. note:: or_like() was formerly known as orlike(), which has been removed.
.. note:: ``or_like()`` was formerly known as ``orlike()``, which has been removed.

$this->db->not_like()
=====================

This function is identical to **like()**, except that it generates NOT
LIKE statements::
This method is identical to ``like()``, except that it generates
NOT LIKE statements::

$this->db->not_like('title', 'match'); // WHERE title NOT LIKE '%match%
$this->db->not_like('title', 'match'); // WHERE `title` NOT LIKE '%match% ESCAPE '!'

$this->db->or_not_like()
========================

This function is identical to **not_like()**, except that multiple
This method is identical to ``not_like()``, except that multiple
instances are joined by OR::

$this->db->like('title', 'match');
$this->db->or_not_like('body', 'match');
// WHERE title LIKE '%match% OR body NOT LIKE '%match%'
// WHERE `title` LIKE '%match% OR `body` NOT LIKE '%match%' ESCAPE '!'

$this->db->group_by()
=====================
Expand Down Expand Up @@ -1054,4 +1055,4 @@ run the query::
$data = $this->db->get()->result_array();

// Would execute and return an array of results of the following query:
// SELECT field1, field1 from mytable where field3 = 5;
// SELECT field1, field1 from mytable where field3 = 5;

0 comments on commit 4173823

Please sign in to comment.