簡體   English   中英

將三個表中的關系結果放入一個嵌套數組中

[英]getting relational results from three tables into one nested array


我在谷歌上搜索了我的問題的解決方案,但修女幫助了我。 在這里,我有三個表itemsfeedsimages 每個項目都有一個提要和一個或多個圖像。 我有3個功能。 一種是從 items 表中返回記錄,第二種是接收 feeds_id (items 表中的外鍵)然后從 feeds 表中返回記錄。 第三個函數是返回所有與items_id相關的圖片。

這些功能是:
* 獲取數據庫中的所有項目:

 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();
   }

然后我有以下代碼來調用這些函數並以json格式顯示結果:

  $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": ""
                         },
                         {
                          ....
                          }
                        ] 
        }]

我的意思是每個項目數組都應該包含來自 get_feeds 和 get_images 函數的相關數據的嵌套數組。 取而代之的是,我得到如下回復:

//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(
          )
   );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM