2015-09-27 34 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(
     ) 
    );