Php 在数据库Codeigniter中连接三个表

Php 在数据库Codeigniter中连接三个表,php,database,codeigniter,join,Php,Database,Codeigniter,Join,我有三个表cases,users,user_role,cases表中有user_id和user_role_id,我想将两个表users和user_role与cases连接起来,将user_id转换为users表中的用户名,并将user_role_id转换为user_role表中的用户名,我在其中一个表中成功,但在连接另一个表时失败 在models/cases\u model.php中 function get_all() { $this->db->where('C.

我有三个表cases,users,user_role,cases表中有user_id和user_role_id,我想将两个表users和user_role与cases连接起来,将user_id转换为users表中的用户名,并将user_role_id转换为user_role表中的用户名,我在其中一个表中成功,但在连接另一个表时失败

在models/cases\u model.php中

function get_all()
    {

    $this->db->where('C.is_archived',0);
    $this->db->select('C.*,U.name client');
        $this->db->join('users U', 'U.id = C.client_id', 'LEFT');
        $this->db->join('user_role UR', 'UR.id = C.city_case', 'LEFT');
    return $this->db->get('cases C')->result();

    }
在controllers/cases.php中

function index(){

        $data['cases'] = $this->cases_model->get_all();
        $data['courts'] = $this->cases_model->get_all_courts();
        $data['clients'] = $this->cases_model->get_all_clients();
        $data['locations'] = $this->location_model->get_all();
        $data['stages'] = $this->case_stage_model->get_all();
        $data['city'] = $this->cases_model->get_name_city();
        $data['page_title'] = lang('case');
        $data['body'] = 'case/list';
        $this->load->view('template/main', $data);  
    }
在view/list.php中

<?php if(isset($cases)):?>
     <tbody>
      <?php $i=1;foreach ($cases as $new){?>
       <tr class="gc_row">
      <td><?php echo $i?></td>

        <td class="small-col">                          
        <?php if($new->is_starred==0){ ?>
        <a href="" at="90" class="Privat"><span style="display:none"><?php echo $new->id?></span>
        <i class="fa fa-star-o"></i></a>
        <?php 
            }else{
            ?>
        <a href="" at="90" class="Public"><span style="display:none"><?php echo $new->id?></span>
        <i class="fa fa-star"></i></a>
        <?php
         }?>
        </td>
        <td><?php echo $new->title?></td>
        <td><?php echo $new->case_no?></td>
        <td><?php echo $new->client?></td>
        <td><?php echo $new->city_case?></td>

我这里不做sql部分。但如果CI的方法是任务的拦截器,那么您应该使用简单的解决方案,而不是CI的PDO方法

简单用词

 $this->db->query({some complex query as String});

您可以尝试此查询以获得所需的输出

            $this->db->select('*');
            $this->db->from('Cases c'); 
            $this->db->join('Users u', 'u.user_id=c.user_id', 'left');
            $this->db->join('user_role r', 'r.user_id=c.user_id', 'left');
            $this->db->where('c.user_id',$id);
            $query = $this->db->get();
现在,在这里,join user_id被视为主键..还有一件事您可以用所需的列名替换*

谢谢