2016-05-23 21 views
0

我有三个表格用户user_role,在case表中有user_id和user_role_id,我想加入两个表users和user_role,用例将user_id转换为他在user表中的名字并转换user_role_id以他的名字在user_role的表,我在他们中的一个成功,但加入另一个表时,我zhcon失败加入数据库Codeigniter中的三个表

in models/cases_model.php 
 

 

 
function get_all() 
 
\t { 
 

 
\t $this->db->where('C.is_archived',0); 
 
\t $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'); 
 
\t return $this->db->get('cases C')->result(); 
 
       \t 
 
\t }
in controllers/cases.php 
 

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

 
<?php if(isset($cases)):?> 
 
    <tbody> 
 
     <?php $i=1;foreach ($cases as $new){?> 
 
     <tr class="gc_row"> 
 
     <td><?php echo $i?></td> 
 
\t \t \t \t \t \t \t \t \t 
 
\t \t <td class="small-col"> \t \t \t \t \t \t \t 
 
\t \t <?php if($new->is_starred==0){ ?> 
 
\t \t <a href="" at="90" class="Privat"><span style="display:none"><?php echo $new->id?></span> 
 
\t \t <i class="fa fa-star-o"></i></a> 
 
\t \t <?php 
 
\t \t \t }else{ 
 
\t \t \t ?> 
 
\t \t <a href="" at="90" class="Public"><span style="display:none"><?php echo $new->id?></span> 
 
\t \t <i class="fa fa-star"></i></a> 
 
\t \t <?php 
 
     }?> 
 
\t \t </td> 
 
     <td><?php echo $new->title?></td> 
 
\t \t <td><?php echo $new->case_no?></td> 
 
\t \t <td><?php echo $new->client?></td> 
 
\t  <td><?php echo $new->city_case?></td>

回答

0

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

简单的文字使用

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

你可以试试这个查询来获取所需的现在输出

  $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是consiodered为主key..and一两件事可以更换*需要列名!

谢谢..