[英]Codeigniter parentheses in Active Record query
我有這個ActiveRecord來生成一個查詢,
$this->purchase_requisition_model
->where('deleted','1')
->likes('to',$sapfvalue,'both')
->likes('date',$sapfvalue,'both')
->likes('request_by',$sapfvalue,'both')
->likes('deliver_to',$sapfvalue,'both')
->likes('name',$sapfvalue,'both')
->likes('telephone',$sapfvalue,'both')
->likes('designation',$sapfvalue,'both')
->likes('budget_status',$sapfvalue,'both')
->find_all();
上面的ActiveRecord將產生以下查詢,
SELECT * FROM (`purchase_requisition`)
WHERE `deleted` = '1'
AND `to` LIKE '%fg%'
OR `date` LIKE '%fg%'
OR `request_by` LIKE '%fg%'
OR `deliver_to` LIKE '%fg%'
OR `name` LIKE '%fg%'
OR `telephone` LIKE '%fg%'
OR `designation` LIKE '%fg%'
OR `budget_status` LIKE '%fg%'
但是如何使用ActiveRecord生成以下查詢?
SELECT * FROM (`purchase_requisition`)
WHERE `deleted` = '1'
AND ( `to` LIKE '%fg%'
OR `date` LIKE '%fg%'
OR `request_by` LIKE '%fg%'
OR `deliver_to` LIKE '%fg%'
OR `name` LIKE '%fg%'
OR `telephone` LIKE '%fg%'
OR `designation` LIKE '%fg%'
OR `budget_status` LIKE '%fg%' )
正如@M Khalid Junaid指出的那樣,Codeigniter的活動記錄庫不支持分組where子句。 您可以使用“where”創建變通方法,同時阻止Codeigniter自動轉義查詢:
$escaped_sapfvalue = $this->db->escape( $sapfvalue );
$this->purchase_requisition_model
->where('deleted','1')
->where("( `to` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `date` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `request_by` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `deliver_to` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `name` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `telephone` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `designation` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->where("OR `budget_status` LIKE '%{$escaped_sapfvalue}%' )", null, FALSE)
->find_all();
請注意我是如何手動轉義變量以防止SQL注入的。 還要注意第一個和最后一個“LIKE”如何分別包括左括號和右括號。
雖然這有效,但它有很多重復的代碼。 循環遍歷數組會更優雅:
$escaped_sapfvalue = $this->db->escape( $sapfvalue );
$or_like = '';
foreach( $column_list as $column ) {
// If it's not the first column, add 'OR'
if ( strlen($or_like) > 0 ) {
$or_like .= ' OR ';
}
// Concatenate manually escaped columns and rows
$escaped_column = $this->db->escape( $column );
$or_like .= "`{$escaped_column}` LIKE '%{$escaped_sapfvalue}%'";
}
// Add grouping parenthesis
$grouped_or_like = "( {$or_like} )";
// Build the query
$this->purchase_requisition_model
->where('deleted','1')
->where( $grouped_or_like, null, false )
->find_all();
編輯:雖然我沒有測試過,但我認為這也應該有效:
$escaped_sapfvalue = $this->db->escape( $sapfvalue );
$this->purchase_requisition_model
->where('deleted','1')
->where("( `to` LIKE '%{$escaped_sapfvalue}%'", null, FALSE)
->or_like('date',$sapfvalue,'both')
->or_like('request_by',$sapfvalue,'both')
->or_like('deliver_to',$sapfvalue,'both')
->or_like('name',$sapfvalue,'both')
->or_like('telephone',$sapfvalue,'both')
->or_like('designation',$sapfvalue,'both')
->where("OR `budget_status` LIKE '%{$escaped_sapfvalue}%' )", null, FALSE)
->find_all();
選擇最適合你的方法。
您也可以使用查詢分組 。
代碼如下所示:
$this->db->where('deleted', 1);
$this->db->group_start();
$this->db->or_like('to', $sapfvalue, 'both')
$this->db->or_like('date', $sapfvalue, 'both')
$this->db->or_like('request_by', $sapfvalue, 'both')
$this->db->or_like('deliver_to', $sapfvalue, 'both')
$this->db->or_like('name', $sapfvalue, 'both')
$this->db->or_like('telephone', $sapfvalue, 'both')
$this->db->or_like('designation', $sapfvalue, 'both')
$this->db->or_like('budget_status', $sapfvalue, 'both')
$this->db->group_end();
$q = $this->db->get('table')->result();
使用group_start和group_end確保之間的or_like語句的這一部分將進入括號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.