将三个表中的关系结果获取到一个嵌套数组中


getting relational results from three tables into one nested array


我在谷歌上搜索问题的解决方案,但修女帮了我。这里我有三个表itemsfeedsimages。每个项目都有一个提要和一个或多个图像。我有三个功能。一种是从items表返回记录,第二种是接收feeds_id(items表中的外键(,然后从feeds表返回记录。第三个函数是返回与itemsID相关的所有图像。

这些功能是:
*获取数据库中的所有项目:

 function get_items(){
  return  $query = Database::getInstance('db')
        ->table('items')
        ->columns(
            'id',
            'items.rowid',
            'items.feed_id as feed_id',
            'title' )
        ->findAll();
}


*从提要表中获取提要数据:

function get_feeds($id){
   return  $query = Database::getInstance('db')
        ->table('feeds')
        ->eq('id',$id)
         ->findAll();
}


*从图像表中获取图像数据:

function get_images($id){
   return  $query = Database::getInstance('db')
        ->table('images')
        ->columns('items_id','src as image_url',
            'title as image_title',
            'alt')
        ->eq('items_id',$id)
        ->findAll();
   }

然后,我有以下代码来调用这些函数,并以jsonformat:显示结果

  $response['items'] = array();
  $response['feeds'] = array();
  $response['images'] = array();   
  foreach ($items = get_items() as $item) {
      $response['items'][] = array(
            'id' => (int)$item['rowid'],
            'feed_id' => (int)$item['feed_id'],
            'title' => $item['title'],
       );
      foreach ($feeds = get_feeds((int)$item['feed_id']) as $feed) {
         $response['feeds'][] = array(
            'title' => $feed['title'],
            'logo_url' => $feed['logo_url'],
            'site_url' => $feed['site_url'],
          );
        }
       foreach ($images = get_images($item['id']) as $image) {
         $response['images'][] = array(
            'id' => $image['items_id'],
            'url' => $image['image_url'],
            'thumb' =>  $_SERVER['SERVER_NAME'] . /myServer/images/thumbs/'. 'thumb_'.basename($image['image_url']),
             'title' => $image['image_title'],
             'alt' => $image['alt']
                );
            }
        }
        echo json_encode($response, JSON_PRETTY_PRINT);

因此,我的期望是获得json输出,如:

"items": [
        {
            "id": ,
            "feed_id": 
            "title":
            "feeds": [
                      {
                       "title": ,
                       "logo_url": ,
                       "site_url": "
                      }
                    ]
             "images": [
                         {
                          "id": ,
                          "url": ",
                          "thumb": 
                          "title": "",
                          "alt": ""
                         },
                         {
                          ....
                          }
                        ] 
        }]

我的意思是,每个项数组都应该包括来自getfeeds和getimages函数的相关数据的嵌套数组。相反,我得到的回复是:

//here i select two items from my db
    "items": [
                { //first_item
                    "id": ,
                    "feed_id": 
                    "title":
                 },
                 { //second_item
                    "id": ,
                    "feed_id": 
                    "title":
                 }
              ],
       "feeds": [
                   { // feed data for first item
                     "title": ,
                      "logo_url": ,
                      "site_url": "
                   },
                   { // feed data for second item
                     "title": ,
                      "logo_url": ,
                      "site_url": "
                   }
               ],
                     "images": [
                                 { // image data for first item
                                  "id": ,
                                  "url": ",
                                  "thumb": 
                                  "title": "",
                                  "alt": ""
                                 },
                                 { // other images data 
                                  ....
                                  }
                                ] 
                }]

正如您所看到的,我得到的输出没有保持项目、提要和图像之间的关系,所有这些都是独立显示的。

我的查询很好,但我怀疑foreach语句中有错误。

我可以通过将这些树表连接到一个查询中来解决这个问题,但我不想这样做,因为我需要对每个表的输出进行验证和其他操作。我感谢你的帮助

我找到了解决方案。这很容易:(

它就像:

 $response['items'][] = array(
            'id' => (int)$item['rowid'],
            'feed_id' => (int)$item['feed_id'],
            'title' => $item['title'],
            'feeds' => array(
             )
          'images' => array(
          )
   );