2016-11-05 5 views
2

codeigniter에서 테이블 이름을 매개 변수로 전달하여 테이블을 동적으로 조인하려고합니다. 아래 모델 함수codeigniter에서 테이블을 동적으로 조인하는 방법은 무엇입니까?

function index(){ 
$table=array('branch'=>'branch_id','specialization'=>'spec_branch_id'); 
$this->model->join($table); 
} 

전화 내 컨트롤러의 함수이며,이 난 표 = 분기 같은 동적 테이블 이름을 원하는 위에서 함수로서 내 모델 함수

function join($table){ 

    foreach($table as $table_name=>$table_id){ 
     /*i want here table*/  
     $table1=$table_name; 
    } 
    $this->db->select('*'); 
    $this->db->from(''.$table1.' t1'); 
    $this->db->join(''.$table2.' t2','t1.'.$t1id.'=t2.'.$t2id); 

    return $this->db->get(); 

    echo $this->db->last_query();die; 
} 

이고; table2 = 내 모델의 특수 기능이므로, 해결하도록 도와주세요. 다른 사람도 공유 할 수 있다면 도와주세요.

답변

4

여기 내 맞춤 코드가 동적으로 결합됩니다.

모드 형태.

<?php public function commonGet($options) { 

      $select = false; 
      $table = false; 
      $join = false; 
      $order = false; 
      $limit = false; 
      $offset = false; 
      $where = false; 
      $or_where = false; 
      $single = false; 
      $where_not_in = false; 
      $like = false; 

      extract($options); 

      if ($select != false) 
       $this->db->select($select); 

      if ($table != false) 
       $this->db->from($table); 

      if ($where != false) 
       $this->db->where($where); 

      if ($where_not_in != false) { 
       foreach ($where_not_in as $key => $value) { 
        if (count($value) > 0) 
         $this->db->where_not_in($key, $value); 
       } 
      } 

      if ($like != false) { 
       $this->db->like($like); 
      } 

      if ($or_where != false) 
       $this->db->or_where($or_where); 

      if ($limit != false) { 

       if (!is_array($limit)) { 
        $this->db->limit($limit); 
       } else { 
        foreach ($limit as $limitval => $offset) { 
         $this->db->limit($limitval, $offset); 
        } 
       } 
      } 


      if ($order != false) { 

       foreach ($order as $key => $value) { 

        if (is_array($value)) { 
         foreach ($order as $orderby => $orderval) { 
          $this->db->order_by($orderby, $orderval); 
         } 
        } else { 
         $this->db->order_by($key, $value); 
        } 
       } 
      } 


      if ($join != false) { 

       foreach ($join as $key => $value) { 

        if (is_array($value)) { 

         if (count($value) == 3) { 
          $this->db->join($value[0], $value[1], $value[2]); 
         } else { 
          foreach ($value as $key1 => $value1) { 
           $this->db->join($key1, $value1); 
          } 
         } 
        } else { 
         $this->db->join($key, $value); 
        } 
       } 
      } 


      $query = $this->db->get(); 

      if ($single) { 
       return $query->row(); 
      } 


      return $query->result(); 
     } ?> 

그리고 당신은 두 개의 테이블 조인이 코드를 시도 할 수 있습니다

<?php function index(){ 

    $option = array(
    'table' => 't1', 
    'join' => array('t2' => 't2.id = t1.id') 
    ); 
    $this->model->commonGet($option); 
    }?> 
+1

는 센을 @pawan 감사합니다 –

0

보기 인덱스 페이지입니다.

컨트롤러 기능.

function index(){   
$table=array('branch'=>'branch_id','specialization'=>'spec_branch_id'); 
$this->model->join($table); 
} 

모델 기능

function join($tables){ 
$i=1; 
foreach($tables as $table_name=>$table_id){ 
    ${'table'.$i}=$table_name; 
    ${'t'.$i.'id'}=$table_id; 
    $i++; 
} 

$this->db->select('*'); 
$this->db->from(''.$table1.' t1'); 
$this->db->join(''.$table2.' t2','t1.'.$t1id.'=t2.'.$t2id); 

//$this->output->enable_profiler(TRUE); 
return $this->db->get();   
} 
관련 문제