【问题标题】:Join three tables in database Codeigniter连接数据库 Codeigniter 中的三个表
【发布时间】:2016-05-23 12:29:30
【问题描述】:

我有三个表 case,users,user_role ,case 表中有 user_id 和 user_role_id ,我想将两个表 users 和 user_role 与 case 连接起来,将 user_id 转换为他在 users 表中的名字,并将 user_role_id 转换为他在 user_role 中的名字表,我在其中一个成功,但是当加入另一个表时我失败了

在模型/cases_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();

    }

在控制器/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);  
    }

在视图/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>

【问题讨论】:

    标签: php database codeigniter join


    【解决方案1】:

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

    简单的单词使用

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

    【讨论】:

      【解决方案2】:

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

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

      现在,在这里加入 user_id 被认为是主键..还有一件事你可以用所需的列名替换 *!

      谢谢..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-20
        • 1970-01-01
        • 2012-01-06
        相关资源
        最近更新 更多