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

时间:2018-08-01 06:39:47

标签: 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

我希望你们能帮助我。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

    $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();

答案 1 :(得分:0)

public function getRequestsToProcess($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("((ns_request_info.line1=".$depth." AND ns_request_info.line1_action='PENDING') OR (ns_request_info.line2=".$depth." AND ns_request_info.line2_action='PENDING') OR (ns_request_info.line3=".$depth." AND ns_request_info.line3_action='PENDING') OR (ns_request_info.line4=".$depth." AND ns_request_info.line4_action='PENDING') OR (ns_request_info.line5=".$depth." AND ns_request_info.line5_action='PENDING'))");
        $this->db->where("(ns_request_info.status='PENDING' OR ns_request_info.status='ONGOING')");
        if($departmentGroup!='HIGHER TIER'){ $this->db->where('ns_staff_info.department_group', $departmentGroup); }
        $this->db->order_by('ns_request_info.idx', 'desc');
        $query = $this->db->get();
        return $query->result();
    }