我想将codeigniter中的此查询转换为Ignited datatable query

时间:2016-02-26 07:45:08

标签: codeigniter datatable codeigniter-2 codeigniter-3

我不知道如何将这个长模型函数转换为点燃数据表查询。我用点燃的数据表完成了小的选择功能。任何人都知道??? 我不知道如何隐蔽它。有任何功能吗?

   public function fetch_data() {

              $this->db->from('jil_requirements');
              $this->db->join('jil_users', 'jil_requirements.rqm_userid=jil_users.usr_id', 'left');
              $this->db->join('jil_merchants', 'jil_requirements.rqm_createdempid=jil_merchants.mer_id', 'left');
              $this->db->where('jil_requirements.rqm_permission!=', '4');
              $this->db->order_by("jil_requirements.rqm_id", "desc");
              $query =  $this->db->get();  
              if ($query->num_rows() > 0) {
              foreach ($query->result() as $row) {
                 $this->db->select('count(jil_mrorfq.rfq_requirementid) as total'); 
                $this->db->from('jil_mrorfq'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=jil_mrorfq.rfq_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query2= $this->db->get()->row_object();
                 $row->total_count = $query2->total;    
                  $this->db->select('count(jil_quotemjdetail.qud_requirementid) as quotemjtotal'); 
                $this->db->from('jil_quotemjdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=jil_quotemjdetail.qud_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query3= $this->db->get()->row_object();
                  $row->quotemj_total = $query3->quotemjtotal;

                   $this->db->select('count(jil_quotejcdetail.qud_requirementid) as quotejctotal'); 
                $this->db->from('jil_quotejcdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id= jil_quotejcdetail.qud_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query4= $this->db->get()->row_object();
                  $row->quotejc_total = $query4->quotejctotal;

                    $this->db->select('count( jil_pocjdetail.pocd_requirementid) as pocjtotal'); 
                $this->db->from('jil_pocjdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=  jil_pocjdetail.pocd_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query5= $this->db->get()->row_object();
                  $row->pocj_total = $query5->pocjtotal;

                   $this->db->select('count(jil_pojmdetail.pojd_requirementid) as pojmtotal'); 
                $this->db->from('jil_pojmdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=  jil_pojmdetail.pojd_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query6= $this->db->get()->row_object();
                  $row->pojm_total = $query6->pojmtotal;


                 $data[] = $row;
                        }
                       return $data;
                    }
                    return false;
               }

3 个答案:

答案 0 :(得分:1)

 $this->db->select('count(jil_quotemjdetail.qud_requirementid) as quotemjtotal'); 
                $this->db->from('jil_quotemjdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=jil_quotemjdetail.qud_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query3= $this->db->get()->row_object();
                  $row->quotemj_total = $query3->quotemjtotal;

                   $this->db->select('count(jil_quotejcdetail.qud_requirementid) as quotejctotal'); 
                $this->db->from('jil_quotejcdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id= jil_quotejcdetail.qud_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query4= $this->db->get()->row_object();
                  $row->quotejc_total = $query4->quotejctotal;

                    $this->db->select('count( jil_pocjdetail.pocd_requirementid) as pocjtotal'); 
                $this->db->from('jil_pocjdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=  jil_pocjdetail.pocd_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query5= $this->db->get()->row_object();
                  $row->pocj_total = $query5->pocjtotal;

                   $this->db->select('count(jil_pojmdetail.pojd_requirementid) as pojmtotal'); 
                $this->db->from('jil_pojmdetail'); 
               $this->db->join('jil_requirements', 'jil_requirements.rqm_id=  jil_pojmdetail.pojd_requirementid', 'left'); 
               $this->db->where('jil_requirements.rqm_id',$row->rqm_id);
                  $query6= $this->db->get()->row_object();
                  $row->pojm_total = $query6->pojmtotal;


                 $data[] = $row;
                        }
                       return $data;
                    }
                    return false;
               }

答案 1 :(得分:0)

首先,您必须加载点燃的dataTables库。

    $this->load->library('datatables');

OR

将其添加到Autoloader,以便在Codeigniter应用程序启动时自动加载。

之后,全部改变

      $this->db->
在您的查询中

     $this->datatables

最后,您必须使用它来编码json数组中的数据

      $this->datatables->generate();

Lemme知道我是否还能提供帮助

<强>更新

public function fetch_data() {

          $this->datatables->from('jil_requirements');
          $this->datatables->join('jil_users',         'jil_requirements.rqm_userid=jil_users.usr_id', 'left');
          $this->datatables->join('jil_merchants', 'jil_requirements.rqm_createdempid=jil_merchants.mer_id', 'left');
          $this->datatables->where('jil_requirements.rqm_permission!=', '4');
          $this->datatables->order_by("jil_requirements.rqm_id", "desc");
          $query =  $this->datatables->get();  

         if ($query->num_rows() > 0) {

          foreach ($query->result() as $row) {
             $this->datatables->select('count(jil_mrorfq.rfq_requirementid) as total'); 
            $this->datatables->from('jil_mrorfq'); 
           $this->datatables->join('jil_requirements', 'jil_requirements.rqm_id=jil_mrorfq.rfq_requirementid', 'left'); 
           $this->datatables->where('jil_requirements.rqm_id',$row->rqm_id);
              $query2= $this->datatables->get()->row_object();
             $row->total_count = $query2->total;    
              $this->datatables->select('count(jil_quotemjdetail.qud_requirementid) as quotemjtotal'); 
            $this->datatables->from('jil_quotemjdetail'); 
           $this->datatables->join('jil_requirements', 'jil_requirements.rqm_id=jil_quotemjdetail.qud_requirementid', 'left'); 
           $this->datatables->where('jil_requirements.rqm_id',$row->rqm_id);
              $query3= $this->datatables->get()->row_object();
              $row->quotemj_total = $query3->quotemjtotal;

               $this->datatables->select('count(jil_quotejcdetail.qud_requirementid) as quotejctotal'); 
            $this->datatables->from('jil_quotejcdetail'); 
           $this->datatables->join('jil_requirements', 'jil_requirements.rqm_id= jil_quotejcdetail.qud_requirementid', 'left'); 
           $this->datatables->where('jil_requirements.rqm_id',$row->rqm_id);
              $query4= $this->datatables->get()->row_object();
              $row->quotejc_total = $query4->quotejctotal;

                $this->datatables->select('count( jil_pocjdetail.pocd_requirementid) as pocjtotal'); 
            $this->datatables->from('jil_pocjdetail'); 
           $this->datatables->join('jil_requirements', 'jil_requirements.rqm_id=  jil_pocjdetail.pocd_requirementid', 'left'); 
           $this->datatables->where('jil_requirements.rqm_id',$row->rqm_id);
              $query5= $this->datatables->get()->row_object();
              $row->pocj_total = $query5->pocjtotal;

               $this->datatables->select('count(jil_pojmdetail.pojd_requirementid) as pojmtotal'); 
            $this->datatables->from('jil_pojmdetail'); 
           $this->datatables->join('jil_requirements', 'jil_requirements.rqm_id=  jil_pojmdetail.pojd_requirementid', 'left'); 
           $this->datatables->where('jil_requirements.rqm_id',$row->rqm_id);
              $query6= $this->datatables->get()->row_object();
              $row->pojm_total = $query6->pojmtotal;

               echo $this->datatables->generate();

           }

答案 2 :(得分:0)

在你的控制器中使用它,看看它是否正常工作

public function fetch_data() 
{ 
 $this->load->library("datatables");
 $query =  $this->datatables
        ->select('jil_requirements.*,count(jil_mrorfq.rfq_requirementid) as total,count(jil_quotemjdetail.qud_requirementid) as quotemjtotal,count(jil_quotejcdetail.qud_requirementid) as quotejctotal,count( jil_pocjdetail.pocd_requirementid) as pocjtotal,count(jil_pojmdetail.pojd_requirementid) as pojmtotal')
        ->from('jil_requirements')
        ->join('jil_users', 'jil_requirements.rqm_userid=jil_users.usr_id', 'left')
        ->join('jil_merchants', 'jil_requirements.rqm_createdempid=jil_merchants.mer_id', 'left')
        ->join('jil_mrorfq', 'jil_mrorfq.rfq_requirementid = jil_requirements.rqm_id', 'left')
        ->join('jil_quotemjdetail', 'jil_quotemjdetail.qud_requirementid = jil_requirements.rqm_id', 'left')
        ->join('jil_pocjdetail', 'jil_pocjdetail.pocd_requirementid = jil_requirements.rqm_id', 'left')
        ->join('jil_pojmdetail', 'jil_pojmdetail.pojd_requirementid = jil_requirements.rqm_id', 'left')
        ->where('jil_requirements.rqm_permission!=', '4');  
    echo $this->datatables->generate();
}