【发布时间】:2019-12-11 00:40:36
【问题描述】:
我有 2 个数据库表 'super_admin_staff' 和 'super_admin_roles'
员工表的组成如下:
id
name
email
phone
role
角色表的组成如下:
id
role
角色表中的 id 列是唯一的,并且与人员表的“角色”列相关联。
目前我正在使用模型从数据库中获取所有角色:
function superAdminStaffRoles()
{
$this->db->select('*');
$this->db->from('super_admin_roles');
$query = $this->db->get();
$result = $query->result();
return $result;
}
在我的控制器中,我将它传递给我的视图,如下所示:
public function superAdminStaffRoles()
{
if ($this->isSuperAdmin() != true) {
$this->loadThis();
} else {
$this->load->model('super_admin_staff_model');
$data['userRecords'] = $this->super_admin_staff_model->superAdminStaffRoles();
$this->global['pageTitle'] = 'Staff Roles';
$this->global['pageDesc'] = 'Add or Edit new Staff Roles and Permissions';
$this->loadViews("super_admin/staff_roles", $this->global, $data, null);
}
}
然后在我的视图中显示数据,例如:
<tbody>
<?php
if (!empty($userRecords)) {
foreach ($userRecords as $record) {
?>
<tr>
<td><?php echo $record->role ?></td>
<td>where want to loop through the association</td>
<td class="text-center">
<a class="btn btn-sm btn-info" href="<?php echo base_url() ?>/EditRole/<?php echo $record->id; ?>"><i class="fas fa-edit"></i></a>
<a class="btn btn-sm btn-danger deleteUser" href="<?php echo base_url() ?>/DeleteRole/<?php echo $record->id; ?>"><i class="fas fa-trash"></i></a>
</td>
</tr>
<?php
}
}
?>
</tbody>
如何在模型中获取我需要的其他信息来显示与该角色相关联的员工人数?我想我可能需要加入表格,但我对加入了解不多。
可以在同一个查询中完成吗?
结果应该是这样的:
Staff table
|id | name | role |
.......................
|1 | staff1 | 1 |
|2 | staff2 | 1 |
|3 | staff3 | 2 |
|4 | staff4 | 3 |
|5 | staff5 | 3 |
Role Table
|id | role |
...............
|1 | role 1 |
|2 | role 2 |
|3 | role 3 |
结果应该是这样的:
|Staff Role | Assigned |
........................
| Role1 | 2 |
| Role2 | 1 |
| Role3 | 2 |
任何帮助表示赞赏
【问题讨论】:
标签: php database codeigniter join count