[英]getting relational results from three tables into one nested array
i have googled for solution to my problem but nun helped me.我在谷歌上搜索了我的问题的解决方案,但修女帮助了我。 here i have three tables
items
, feeds
and images
.在这里,我有三个表
items
、 feeds
和images
。 each item has one feed and one or more images.每个项目都有一个提要和一个或多个图像。 i have 3 functions.
我有3个功能。 one is to return records from items table the second one receives feeds_id (foreign key in items table) then return records from feeds table.
一种是从 items 表中返回记录,第二种是接收 feeds_id (items 表中的外键)然后从 feeds 表中返回记录。 the third function is to return all images related to items_id.
第三个函数是返回所有与items_id相关的图片。
those functions are :这些功能是:
* To get all items in database: * 获取数据库中的所有项目:
function get_items(){
return $query = Database::getInstance('db')
->table('items')
->columns(
'id',
'items.rowid',
'items.feed_id as feed_id',
'title' )
->findAll();
}
* To get feed data from feeds table : * 要从提要表中获取提要数据:
function get_feeds($id){
return $query = Database::getInstance('db')
->table('feeds')
->eq('id',$id)
->findAll();
}
* To get image data from images table : * 要从图像表中获取图像数据:
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();
}
Then i have the following code to call those function and display the result in jsonformat:然后我有以下代码来调用这些函数并以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);
so, my expectation is to get json output like:所以,我的期望是得到 json 输出,如:
"items": [
{
"id": ,
"feed_id":
"title":
"feeds": [
{
"title": ,
"logo_url": ,
"site_url": "
}
]
"images": [
{
"id": ,
"url": ",
"thumb":
"title": "",
"alt": ""
},
{
....
}
]
}]
i mean each item array should include nested arrays of its related data coming from get_feeds and get_images functions.我的意思是每个项目数组都应该包含来自 get_feeds 和 get_images 函数的相关数据的嵌套数组。 instead of that, i get response like :
取而代之的是,我得到如下回复:
//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
....
}
]
}]
as you see i am getting output without keeping relation between items, feeds and images, all of them are shown independently.如您所见,我在没有保持项目、提要和图像之间的关系的情况下获得输出,所有这些都是独立显示的。
my queries are fine but i am suspecting error in my foreach
statements.我的查询很好,但我怀疑我的
foreach
语句中有错误。
i could fix this issue by joining those tree tables in one query, but i don't want to do that because i need to do validation and other operations to output comes from each table.我可以通过在一个查询中加入这些树表来解决这个问题,但我不想这样做,因为我需要进行验证和其他操作来输出来自每个表。 i appreciate your help
我感谢您的帮助
i found the solution.我找到了解决方案。 it is very easy :)
这很容易:)
it is just like:它就像:
$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.