2014-11-24 46 views
0

我很困難如何顯示或顯示具有多個關係的表,並返回其表/用戶關係inteadt。使用codeigniter返回REST API請求中的表和關係結果

Forexample:

我有公開表和版本表..related到1公開可以有多個/衆問題。 ,當我想加載例如

http://restapi.url/publications/format/json

我想看到的代碼點火出版以及所有相關的問題。這是我在我的控制器代碼..

function publications_get(){ 
     $this->output->enable_profiler(TRUE); 
     $this->db->select(); 
     $this->db->from('publication'); 
     $return = $this->db->get(); 

     if($return!= null) { 

      $this->response($return->result_array(),200); //success 
     } else { 

      $this->response(NULL,404); 
     } 

伸出來是這樣的:

<officer id="1"> 
    <name>James T. Kirk</name> 
    <rank>Captain</rank> 
    <subordinates> 
     <link ref="http://ncc1701/api/officers/2"> 
     <link ref="http://ncc1701/api/officers/3"> 
    </subordinates> 
</officer> 

回答

0

使用兩個查詢,一個對所有出版物(就像現在),併爲每一個問題由發佈

function publications_get(){ 
     $this->output->enable_profiler(TRUE); 
     $this->db->select(); 
     $this->db->from('publication'); 
     $return = $this->db->get(); 
     $publications = $return->result_array(); //success 
     foreach($publications as $index => $publication){ 
      $publications[$index]['issues'] = issues_get($publication['id']); 
     } 
     $this->response($publications,200); 
} 

獲得刊物中的問題

function issues_get($id_publication){ 
     $this->output->enable_profiler(TRUE); 
     $this->db->select(); 
     $this->db->from('isssues'); 
     $this->db->where('id_publication', $id_publication); 
     $return = $this->db->get(); 
     $this->response($return->result_array(),200); //success 
}