douji6940 2018-07-08 13:03
浏览 64

如何从像mongodb这样的mysql链接数据,以便将json传递给api

I have a few tables in my MySQL database which are interconnected with foreign keys and I want to retrieve data from these for my REST API. So far I am successfully able to do it but I want to chain/group data from MYSQL for my REST API. Currently, I am getting my data like this

{
   "status":"success",
   "data":[
      {
         "floor_id":"1",
         "fk_client_id":"1",
         "floor_name":"first",
         "floor_created_on":"2018-07-07 05:20:06",
         "floor_active":"1",
         "room_id":"1",
         "fk_floor_id":"1",
         "fk_type_id":"1",
         "room_number":"101",
         "room_created_on":"2018-07-07 05:20:44",
         "room_active":"1"
      },
      {
         "floor_id":"1",
         "fk_client_id":"1",
         "floor_name":"first",
         "floor_created_on":"2018-07-07 05:20:06",
         "floor_active":"1",
         "room_id":"4",
         "fk_floor_id":"1",
         "fk_type_id":"2",
         "room_number":"104",
         "room_created_on":"2018-07-08 17:49:35",
         "room_active":"1"
      },
      {
         "floor_id":"3",
         "fk_client_id":"1",
         "floor_name":"third",
         "floor_created_on":"2018-07-07 05:20:21",
         "floor_active":"1",
         "room_id":"3",
         "fk_floor_id":"3",
         "fk_type_id":"3",
         "room_number":"301",
         "room_created_on":"2018-07-07 05:21:26",
         "room_active":"1"
      }
   ]
}

Demo Link of the current response format

But I believe this is not the right approach, as you can see Floors data is being repeated for each Room and also this structure might be difficult to parse at the client side. I have worked with API data from MongoDB and I think a similar structure would be the best one for my data but I have no idea how to do it using MYSQL and PHP.

I have formatted the JSON which is given below myself to show how I need the response to be.

{
   "status":"success",
   "data":{
      "floors":[
         {
            "floor_id":"1",
            "fk_client_id":"1",
            "floor_name":"first",
            "floor_created_on":"2018-07-07 05:20:06",
            "floor_active":"1",
            "rooms":[
               {
                  "room_id":"1",
                  "fk_floor_id":"1",
                  "fk_type_id":"1",
                  "room_number":"101",
                  "room_created_on":"2018-07-07 05:20:44",
                  "room_active":"1"
               },
               {
                  "room_id":"4",
                  "fk_floor_id":"1",
                  "fk_type_id":"2",
                  "room_number":"104",
                  "room_created_on":"2018-07-08 17:49:35",
                  "room_active":"1"
               }
            ]
         },
         {
            "floor_id":"3",
            "fk_client_id":"1",
            "floor_name":"third",
            "floor_created_on":"2018-07-07 05:20:21",
            "floor_active":"1",
            "rooms":[
               {
                  "room_id":"3",
                  "fk_floor_id":"3",
                  "fk_type_id":"3",
                  "room_number":"301",
                  "room_created_on":"2018-07-07 05:21:26",
                  "room_active":"1"
               }
            ]
         }
      ]
   }
}

Demo Link of the required response format

I am using Php Codeigniter framework and here is my data fetching method in model class:

public function getdata($client_id) 
{
    $this - > db - > select('*');
    $this - > db - > from('floor_tb');
    $this - > db - > order_by('floor_tb.floor_id');
    $this - > db - > join('room_tb', 'room_tb.fk_floor_id=floor_tb.floor_id');
    $this - > db - > where("floor_tb.fk_client_id", $client_id);
    $this - > db - > where("floor_tb.floor_active", '1');
    $this - > db - > where("room_tb.room_active", '1');

    $mytable = $this - > db - > get();

    if ($mytable) 
    {
        return $mytable - > result_array();
    } else 
    {
        return false;
    }
}

And this is my code from api controller:

$data = $this - > Data_Model - > getdata($client_id);
  if (!$data) {
      $failmsg = array('status' => 'fail', 'message' => 'no data found');
      $this - > response($failmsg, 201);

  } else {
      $successmsg = array('status' => 'success', 'data' => $data);

      $this - > response($successmsg, 200);

  }

Could someone please help me to achieve the desired ouput.

  • 写回答

1条回答 默认 最新

  • dongpao1926 2018-07-09 15:31
    关注

    Have you looked at MySQL 8's JSON_OBJECTAGG function? it works with both JSON and non-JSON columns

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler