I\'m producing a query like the following using ActiveRecord
SELECT * FROM (`foods`) WHERE `type` = \'fruits\' AND
`tags` LIKE \'%green%\' OR `tags`
From the CI wiki:
The codeignighter ActiveRecord feature allows you to create SQL queries relatively simply and database-independant, however there isno specific support for including parenthesis in an SQL query.
For example when you want a where statement to come out simmilarly to the folowing:
WHERE (field1 = value || field2 = value) AND (field3 = value2 || field4 = value2)
This can be worked around by feeding a string to the CI->db->where() function, in this case you will want to specifically escape your values.
See the following example:
$value=$this->db->escape($value);
$value2=$this->db->escape($value2);
$this->db->from('sometable');
$this->db->where("($field = $value || $field2 = $value)");
$this->db->where("($field3 = $value2 || $field4 = $value2)");
$this->db->get();
A simmilar workaround can be used for LIKE clauses:
$this->db->where("($field LIKE '%$value%' || $field2 LIKE '%$value%')");
$this->db->where("($field3 LIKE '%$value2%' || $field4 LIKE '%$value2%')");
You can't add parentheses in ActiveRecord, but maybe WHERE IN is what you're looking for?
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')
$likes = array (
'0' => 'green'.
'1' => 'blue',
'2' => 'red'
);
$where_like = "(";
$or_counter = 1;
foreach($likes as $like_key => $like_val):
$or_content = ($or_counter > 1) ?'OR': '';
$newlikeval = $this->db->escape_like_str($like_val);
$where_like .= $or_content." `$like_key` LIKE '%$newlikeval%' ";
$or_counter++;
endforeach;
$where_like .= ")";
$this->db->where($where_like);
In the CI 3.0-dev you can add groups in query:
$this->db->select('id, title')
->group_start()
->or_like([ 'title' => $s, 'id' => $s ])
->group_end()
->where([ 'b_del' => 0 ]);
Produces:
SELECT `id`, `title`, `venda1`
FROM `itens`
WHERE
(
`title` LIKE '%a%' ESCAPE '!'
OR `id` LIKE '%a%' ESCAPE '!'
)
AND `b_del` =0
Going off of The Silencer's solution, I wrote a tiny function to help build like conditions
function make_like_conditions (array $fields, $query) {
$likes = array();
foreach ($fields as $field) {
$likes[] = "$field LIKE '%$query%'";
}
return '('.implode(' || ', $likes).')';
}
You'd use it like this:
$search_fields = array(
'field_1',
'field_2',
'field_3',
);
$query = "banana"
$like_conditions = make_like_conditions($search_fields, $query);
$this->db->from('sometable')
->where('field_0', 'foo')
->where($like_conditions)
->get()
use codeigniter 3
$this->db->select('*');
$this->db->from($this->MasterMember);
$this->db->group_start();
$this->db->where($this->IDCardStatus, '1');
$this->db->or_where($this->IDCardStatus, '2');
$this->db->group_end();
if ($searchKey1 != null) {
$this->db->group_start();
$this->db->like($this->MemberID, $searchKey1);
$this->db->or_like($this->FirstName, $searchKey2);
$this->db->or_like($this->LastName, $searchKey3);
$this->db->group_end();
}
$this->db->limit($limit, $offset);
$data = $this->db->get();
this is my native query
SELECT
*
FROM
`Member`
WHERE ( `Member`.`IDCardStatus` = '1' OR `Member`.`IDCardStatus` = '2' )
AND ( `Member`.`MemberID` LIKE '%some_key%' ESCAPE '!' OR `Member`.`FirstName` LIKE '%some_key%' ESCAPE '!' OR `Member`.`LastName` LIKE '%some_key%' ESCAPE '!' )
LIMIT 10