2015-09-27 36 views
-1


我已經搜索瞭解決我的問題,但修女幫我。 這裏我有三個表items,feedsimages。每個項目都有一個Feed和一個或多個圖像。 我有3個功能。一個是從項目表中返回記錄,第二個接收feeds_id(項目表中的外鍵),然後從源表中返回記錄。第三個功能是返回與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(); 
    } 

然後,我有下面的代碼來調用這些功能,並將結果顯示在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": "" 
         }, 
         { 
          .... 
          } 
         ] 
     }] 

我的意思是每個項目陣列應包括來自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聲明中有錯誤。

我可以通過在一個查詢中加入這些樹表來解決此問題,但我不想這樣做,因爲我需要執行驗證和其他操作以輸出來自每個表。 我感謝您的幫助

回答

0

我找到了解決方案。這是很容易:)

它就像:

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