Search code examples
phpcodeigniteractiverecordsql-likecodeigniter-2

$side parameter of CodeIgniter 2.1.0's like() method added "both" wildcards despite being set to "none"


According to the documentation, passing a third parameter of 'none' to the like method should eliminate the use of wildcard characters in a like search query.

I'm doing this, where $search == 'test_username':

$this->db->like('username', $search, 'none');
$this->db->limit(1);
$q = $this->db->get('customers')->row();
var_dump($this->db->last_query());exit;

I expect to see this echoed to the screen:

SELECT * FROM (`ci_customers`) WHERE `username` LIKE 'test_username' LIMIT 1

But I'm getting this instead:

SELECT * FROM (`ci_customers`) WHERE `username` LIKE '%test_username%' LIMIT 1

It seems like the method is ignoring the 3rd parameter or I'm doing something wrong. Any Ideas? I can just write out my query and use the query() method but I'm curious.

I am using CodeIgniter 2.1.0.


Solution

  • It looks as though the code for the "none" is not included in the 2.1.0 version. Have a look at lines 649-692 on https://github.com/EllisLab/CodeIgniter/blob/v2.1.0/system/database/DB_active_rec.php#L639.

    and then look at line 664 of the 2.1.3 version: https://github.com/EllisLab/CodeIgniter/blob/2.1.3/system/database/DB_active_rec.php#L639

    You either need to upgrade, or add this to your DB_active_rec.php file:

    ...
    if ($side == 'none')
    {
      $like_statement = $prefix." $k $not LIKE '{$v}'";
    }
    ...