Codeigniter组织架构JSON输出:递归循环优化方案求助
Got it, I see the issue here—your current code only pulls in direct subordinates for each employee, but doesn't recursively fetch all levels of the hierarchy. Let's fix that with a recursive function to build out the full organizational structure JSON you need, matching the exact format you specified.
Step 1: Update the Model with a Recursive Method
First, we'll modify your model to add a recursive function that fetches not just direct subordinates, but all nested levels of employees under a given manager. We'll also ensure we include the jobDesc field required in your output.
class Api_model extends CI_Model { public function fetch_employees() { $qry = $this->db->get('employees'); return $qry->result_array(); } // Recursive function to get full subordinate hierarchy public function getEmployeeHierarchy($reportsTo) { // Select fields and alias them to match your desired JSON keys $this->db->select('employeeNumber as empNo, CONCAT(firstName, " ", lastName) AS name, jobDesc'); $this->db->from('employees'); $this->db->where('reportsTo', $reportsTo); $query = $this->db->get(); $subordinates = $query->result_array(); // For each subordinate, recursively fetch their own subordinates foreach ($subordinates as &$subordinate) { $subordinate['empUnd'] = $this->getEmployeeHierarchy($subordinate['empNo']); // If a subordinate has no underlings, set empUnd to an empty array (adjust to [{}] if strictly required) if (empty($subordinate['empUnd'])) { $subordinate['empUnd'] = []; } } return $subordinates; } }
Step 2: Update the Controller to Build the Root Hierarchy
Next, we'll adjust the controller to target the root of your organization (e.g., the CEO who has no manager) and build the full hierarchy starting from them. This ensures we return a single top-level object instead of an array of all employees.
public function chart() { // Find the root employee (adjust the WHERE clause to match your database structure) // Example: CEO has reportsTo set to NULL, 0, or a specific ID like '123' $rootEmployee = $this->db->select('employeeNumber as empNo, CONCAT(firstName, " ", lastName) AS name, jobDesc') ->from('employees') ->where('reportsTo IS NULL') // Replace with your root condition if needed ->get() ->row_array(); if ($rootEmployee) { // Attach the full subordinate hierarchy to the root employee $rootEmployee['empUnd'] = $this->api_model->getEmployeeHierarchy($rootEmployee['empNo']); // Handle case where root has no subordinates if (empty($rootEmployee['empUnd'])) { $rootEmployee['empUnd'] = []; } $result = $rootEmployee; } else { // Fallback if no root employee is found $result = []; } // Return properly formatted JSON return $this->output->set_content_type('application/json')->set_output(json_encode($result)); }
Key Notes:
- Recursive Logic: The
getEmployeeHierarchyfunction calls itself for each subordinate, creating a nested structure automatically until there are no more employees to fetch. - Root Employee: Make sure to adjust the
where('reportsTo IS NULL')clause to match your database's actual root employee condition (e.g., some systems usereportsTo = 0or a specific employee ID like123). - JSON Format: The output will now match your requested structure, with each employee having
empNo,name,jobDesc, and a nestedempUndarray of their subordinates.
内容的提问来源于stack exchange,提问作者coy1013

