【问题标题】:Return a table and relationships result on REST API request using codeigniter使用 codeigniter 返回 REST API 请求的表和关系结果
【发布时间】:2014-11-24 12:16:27
【问题描述】:

我遇到了麻烦,如何显示或显示具有多个关系的表格,以仅返回表格/用户的关系。

例如:

我有 Publication Table 和 Issue Table ..related to 1-Publication can have multiple/many-ISSUES. 而且,当我想加载例如

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>

【问题讨论】:

    标签: php json codeigniter


    【解决方案1】:

    使用两个查询,一个用于所有出版物(如现在),一个用于每个出版物的问题

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 2012-06-12
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      相关资源
      最近更新 更多