php - 查找 child 部门的员工 - PHP

标签 php mysql codeigniter recursion activerecord

我正在使用 Codeigniter + MySQL + Active Record 构建一个带有组织结构图的项目。

有部门列为组织树,人员信息的员工,员工角色和 Staff_Departments 我存储匹配项: 部门 - 员工 - 角色

你可以看到下面的结构:

部门 (parent_id 用于构建树)

enter image description here

员工 (原始员工信息)

enter image description here

员工角色 (权重最低,层级最高)

enter image description here

员工部门 (在哪个部门 - 谁 - 什么角色)

enter image description here

在稍后的阶段,一名员工可能属于 2 个或更多具有不同角色的部门。这就是为什么我为多对多使用单独的表 Staff_departments。在这种情况下,让我们保持简单,假设 1 名员工属于 1 个部门。

我正在尝试做的事情:

  1. 部门中的经理(role weight=0 || role_id=1),可以查看在他的部门工作的员工(员工和主管)AND 所有来自部门的员工(员工和主管)是他部门的 child 。树的深度未知。
  2. 主管可以查看仅在其部门工作的员工(仅限员工)。
  3. 员工只能查看他自己。

对于主管和员工来说,这个过程很简单,所以我认为我可以接受。对于经理,我可能必须做一些递归的事情,但每次我开始编写一些代码行时,我都在苦苦挣扎。

我的想法是在我的 Controller 中有一个函数 find_related_staff($staff_id) {},我将传递已登录员工的 ID,它将返回一个包含 ID 的数组他的相关员工。我唯一得到的是登录员工的 ID。

If Manager return IDs of Managers,Supervisors and Employees related in his Department AND Managers,Supervisors and Employees from Child Departments of his Department.

If Supervisor return IDs of Supervisors and Employees related in his Department only.

If Employee return his ID

关于如何实现它的任何想法?

最佳答案

好吧,我认为,为了让事情更容易理解,我们需要把你的问题分解成小块(我只关注你说你真正需要帮助的部分:经理的递归) .

首先,我们获取与用户关联的当前部门进行身份验证。正如您所说,您只有当前签名员工的 ID,所以我们将从它开始。假设用户 ID 已分配给变量 $user_id。

$user_department = $this->db->get_where('staff_departments', ['staff_id' => $user_id])->row();

现在我们有了部门,我们检查一下用户在该部门中的角色是什么。我们将该信息添加到 $user_department 对象:

$user_department->role = $this->db->get_where('staff_roles', ['role_id' => $user_department->role_id])->row();

让我们检查一下用户角色的权重,好吗?如果它是 0,我们就知道它是那个部门的经理,所以我们将递归地查找嵌套的部门及其员工信息。根据您的逻辑指示,我们可以在这里检查用户是否也是主管,并在必要时升级。像这样:

if ($user_department->role->role_weight <= 1) {
    // the user is a supervisor OR a manager, but both those can see, at least, the current department's staff information
    $user_department->staff = $this->db->get_where('staff_departments', ['department_id' => $user_department->department_id]);

    // now is the user a manager? If so, let's find nested departments
    if ($user_department->role->role_weight === 0) {
        $user_department->childs = $this->getChildDepartmentsAndStaffOf($user_department->department_id);
    }
}

您可能会注意到,有一个函数将被递归调用。一定是这样的:

public function getChildDepartmentsAndStaffOf($department_id)
{
    $child_departments = $this->db->get_where('departments', ['parent_id' => $department_id]);

    if (! $child_departments) {
        return null;
    }

    foreach ($child_departments as &$department) {
        $department->staff = $this->db->get_where('staff_departments', ['department_id' => $department->department_id]);

        $department->childs = $this->getChildDepartmentsAndStaffOf($department->department_id);
    }

    return $child_departments;
}

现在,您拥有了所需的结构。我知道这可能会被重构,但我认为这足以得到您的答案并为您指明正确的道路。

希望我有所帮助。

关于php - 查找 child 部门的员工 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43366505/

相关文章:

mysql - MySQL如何处理一张表中的数据并将其插入到另一张表中?

异常类中的 php 父构造函数

php - 用于从管理员创建订单的 WooCommerce 钩子(Hook)

php - 在php和mysql中将blob类型转换为base64时显示半张图像

javascript - 自动刷新表 PHP JavaScript/jQuery

php - 在 Codeigniter 中查找数据库中实际删除或未删除的数据的方法

php - codeigniter 中的错误处理

php - 如何创建多个子菜单及其数据库结构?

php - Laravel 中的 createMany 上的 createMany?

php - 用来自 sql 结果的 php 回显第 30 行?