douzhi1924 2016-11-05 06:52
浏览 59
已采纳

如何在codeigniter中动态连接表?

I want to join table dynamically in codeigniter by passing table name as parameter. below is my controller function to call model function

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

and this is my model function

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;
}

As in above function i want dynamic table name like table1=branch; table2=specializaton in my model function, so please help me to solve and if someone have another also can share.

  • 写回答

2条回答 默认 最新

  • doupeng5320 2016-11-05 07:01
    关注

    here is my custom code dynamically join.

    mode form.

     <?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();
            } ?>
    

    and view index page

     <?php  function index(){  
    
        $option = array(
        'table' => 't1',
        'join' => array('t2' => 't2.id = t1.id')
        );
        $this->model->commonGet($option);
        }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理