[英]One To Many relationship codeigniter/datamapper
I need help on this plz : 我需要这个plz的帮助:
2 tables 2桌
Service : has many Sub_Service Sub_Service : has one Service 服务:有很多Sub_Service Sub_Service:有一个Service
My code : 我的代码:
$services = new Service();
$services->where('org_id', $org_id)->get();
$services->sub_service->get();
foreach($services as $service)
{
echo $service->title;
foreach($service->sub_service as $sub_service)
{
echo $sub_service->title;
}
}
But it doesn't work, if I want to access sub_service i have to take it out of the first loop and do somthing like 但这是行不通的,如果我想访问sub_service,我必须将其从第一个循环中取出并做类似的事情
foreach($services->sub_service as $sub_service)
{
echo $sub_service->title;
}
But this is not what I want, i want to get an array like this one : 但这不是我想要的,我想要一个像这样的数组:
Service 1 服务1
—Subservice 1 —子服务1
—Subservice 3 —子服务3
Service 2 服务2
Service 3 服务3
—Subservice 2 —子服务2
Info i'm using DM 1.8 and CI2. 信息我正在使用DM 1.8和CI2。
Thx for ur help 感谢您的帮助
Try using the 'all' property available in datamapper objects 尝试使用datamapper对象中可用的'all'属性
foreach($services->all as $service)
{
echo $service->title;
foreach($service->sub_service->all as $sub_service)
{
echo $sub_service->title;
}
}
You have to use the include_join_fields method of datamapper in order to create the relationship as you want it. 您必须使用datamapper的include_join_fields方法来创建所需的关系。 In your controller, include this:
在您的控制器中,包括以下内容:
$service = new Service();
$data = $service->get();
$options['records'] = $data;
and then in your view you can do this: 然后您可以执行以下操作:
if(isset($records)){
foreach($records as $service){
echo $service->title;
$subservice = $service->subservice->include_join_fields()->get();
foreach($subservise as $subservice){
if(!is_null($subservice->title)){
echo $subservice->title;
}
}
}
This will give you what you want. 这会给你你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.