我得到了3个表:
网络、公司、部门
网络<表类>id 名称 tbody><<tr>1 Network1 2Network2 表类>
你可以这样称呼它:
$results = Network::find()
->joinWith('company')
->joinWith('company.department')
->where(['network.id' => 1])
->all();
和你的foreach:
if($results){
foreach($results as $result){
if($companies = $result->company){
foreach($companies as $company){ //foreach you companies attached to network One
if($departments = $company->department){
foreach($departments as $department){ //foreach you departments attached to every company
echo $department->name.' (Company: '.$company->name.')';
}
}
}
}
}
}
我认为另一种方式更好
$departments = Department::find()
->leftJoin('company', 'company.id = department.company_id')
->where('company.network_id = 1')
->all();
这取决于你需要展示什么数据。
p。在你的Department
中必须与Company
有关系并且可以使用它