【问题标题】:Codeigniter parentheses in Active Record queryActive Record 查询中的 Codeigniter 括号
【发布时间】:2014-06-26 03:42:44
【问题描述】:

我有这个 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%' )

【问题讨论】:

  • 根据docs,没有 likes() 功能可能是您使用基于 codeigniter 的任何 cms 如果是这样,则正确标记它您没有直接使用活动记录,还分组了活动记录尚不支持的 where 子句,您需要编写原始查询并正确转义输入

标签: php mysql codeigniter activerecord bonfire


【解决方案1】:

正如@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();

选择最适合你的。

【讨论】:

  • 感谢我正在针对数组中定义的所有列对搜索词进行循环,您的示例将帮助我构建一个查询,该查询将允许我强制执行诸如“active = true”之类的 where 条件,而允许我通过文本搜索数组中的任何列。
【解决方案2】:

你也可以使用query grouping

代码将如下所示:

$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 可以确保在 then 之间的这部分 or_like 语句将进入括号。

【讨论】:

    猜你喜欢
    • 2011-09-27
    • 1970-01-01
    • 2016-03-15
    • 2012-04-17
    • 2013-02-22
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多