php - 如何将此 SQL 查询转换为 Codeigniter Active Record?

标签 php mysql codeigniter activerecord

我有这个 SQL 查询,它看起来很困惑,让我思考如何将其转换为 Codeigniter Active Record 查询。

我考虑过像这样逐行循环它:

for ( $i=1 ; $i<=5 ; $i++) {
     ${'line'.$i} = $this->staffrequisition_model->getRequestsToProcess($i,$staffInfo->position_depth,$staffInfo->department_group);
}
$data = array_merge($line1, $line2, $line3, $line4, $line5);

在我的模型中使用此方法

public function getRequestsToProcess($i,$depth,$departmentGroup)
{
    $this->db->select('*, ns_request_info.idx as request_idx, ns_staff_info.idx as staff_idx');    
    $this->db->from('ns_request_info');
    $this->db->join('ns_staff_info', 'ns_request_info.requested_by_idx = ns_staff_info.member_idx');
    $this->db->where('line'.$i.'', $depth);
    $this->db->where('line'.$i.'_action', 'PENDING');
    if($departmentGroup!='HIGHER TIER'){ $this->db->where('ns_staff_info.department_group', $departmentGroup); }
    $this->db->where('status', 'PENDING');
    $this->db->or_where('status', 'ONGOING');
    $this->db->where('line'.$i.'', $depth);
    $this->db->where('line'.$i.'_action', 'PENDING');
    if($departmentGroup!='HIGHER TIER'){ $this->db->where('ns_staff_info.department_group', $departmentGroup); }
    $this->db->order_by('idx', 'desc');
    $query = $this->db->get();
    return $query->result_array();
}

但我意识到对所有这些进行数组合并会破坏结果 id 的排序。

我这里有我的 SQL 查询,但我不知道如何将其转换为 Codeigniter Active Record 查询。

SELECT *, ns_request_info.idx as request_idx, ns_staff_info.idx as staff_idx 

FROM `ns_request_info` 
INNER JOIN `ns_staff_info` 
ON ns_request_info.requested_by_idx = ns_staff_info.member_idx 

WHERE (
    (ns_request_info.line1=1 AND ns_request_info.line1_action='PENDING') OR
 (ns_request_info.line2=1 AND ns_request_info.line2_action='PENDING') OR
 (ns_request_info.line3=1 AND ns_request_info.line3_action='PENDING') OR
 (ns_request_info.line4=1 AND ns_request_info.line4_action='PENDING') OR
 (ns_request_info.line5=1 AND ns_request_info.line5_action='PENDING')
    ) AND (ns_request_info.status='PENDING' OR ns_request_info.status='ONGOING') 
ORDER BY ns_request_info.idx DESC

希望大家能帮帮我。

最佳答案

尝试这样的事情:

    $search_val = 1;
    $action = "PENDING";
    $this->db->where("nri.line1 = $search_val and nri.line1_action = '{$this->db->escape_str($action)}'")
    ->or_where("nri.line2 = $search_val and nri.line2_action = '{$this->db->escape_str($action)}'")
    ->or_where("nri.line3 = $search_val and nri.line3_action = '{$this->db->escape_str($action)}'")
    ->or_where("nri.line4 = $search_val and nri.line4_action = '{$this->db->escape_str($action)}'")
    ->or_where("nri.line5 = $search_val and nri.line5_action = '{$this->db->escape_str($action)}'")
    ->join('ns_staff_info nsi', 'nsi.member_idx = nri.requested_by_idx')
    ->get('ns_request_info nri')->result_array();

关于php - 如何将此 SQL 查询转换为 Codeigniter Active Record?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51626840/

相关文章:

php - Symfony 2 - 从另一个 Controller 调用 Controller

php - 提交表单后如何使下拉菜单值仍处于选中状态?

php - 如何使用 mailgun 批量发送带附件的邮件?

PHP MySQL AUTO_INCRMENT 与 INSERT INTO .. ON DUPLICATE KEY

php - 使用PHP将HTML表单数据插入MySQL数据库

带连接和条件语句的 MySql SELECT

php - codeigniter url 中的访问被拒绝错误具有 index.php

php - Google 的 oauth2-client 在 Codeigniter 中工作

php - 如何将父ID输入到不同表的数据库中

javascript - 如何使用php在mysql数据库数据更改上自动发送短信