php - Active Record 查询中的 Codeigniter 括号

标签 php mysql codeigniter activerecord bonfire

我有这个 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 注入(inject)的。另请注意第一个和最后一个“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();

选择最适合您的。

关于php - Active Record 查询中的 Codeigniter 括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23547011/

相关文章:

mysql - 在 SqlFiddle 上执行触发器存储过程。 mysql

java - 我如何在 JDBC 中使用 WHERE EXISTS 函数

php - 将 codeigniter 迁移到另一台服务器

codeigniter - 如何从 webroot 外部加载 codeigniter font Awesome fontface url?

PHP 5.3 命名空间 我应该使用每个带反斜杠的 PHP 函数吗?

php - 每 3 项和 4 项交替

php - DateTime::createFromFormat - php 格式问题

mysql - 在 MySQL 数据库上使用版本控制 (Git)

security - 考虑到安全性,PHP 框架有哪些优势?

php - 更新 V3 API 的 YouTube PHP 函数