You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中按登录部门加载对应学生数据的问题

解决CodeIgniter部门账号仅加载本部门学生的问题

嘿,我帮你梳理下问题哈~你现在遇到的核心问题是登录的部门账号加载了所有学生数据,而不是仅本部门的,这是因为你的模型查询里没有加入「当前登录用户所属部门」的过滤条件,咱们一步步来修改:

第一步:获取当前登录用户的部门ID

首先在控制器里,你需要从session中取出当前登录用户的Dep_ID(假设你已经把登录用户的信息存在session里了,比如userInfo数组),然后把这个ID传给模型做过滤:

修改控制器的Populate_Data方法:

function Populate_Data() {
    if($this->isDepartment() == TRUE) {
        $this->loadThis();
    } else {
        $searchText = $this->security->xss_clean($this->input->post('searchText'));
        $data['searchText'] = $searchText;
        
        // 关键:获取当前登录用户的部门ID
        $currentDepId = $this->session->userdata('userInfo')['Dep_ID']; 
        // 注意:这里要根据你实际存储登录用户信息的session键名调整
        
        $this->load->library('pagination');
        // 同步修改计数方法,传入部门ID
        $count = $this->department_model->userListingCount($searchText, $currentDepId);
        $returns = $this->paginationCompress ( "userListing/", $count, 10 );
        
        // 调用模型时传入部门ID
        $data['userRecords'] = $this->department_model->Populate_data($searchText, $returns["page"], $returns["segment"], $currentDepId);
        
        $this->global['pageTitle'] = 'HU : User Listing';
        $this->loadViews("department/Students", $this->global, $data, NULL);
    }
}

第二步:修改模型的查询方法,加入部门过滤

原来的模型查询没有过滤条件,所以会返回所有学生。现在我们修改Populate_data方法,接收部门ID并加入WHERE条件,同时可以简化查询(不需要关联department表,直接过滤student表即可):

function Populate_data($searchText = '', $page = null, $segment = null, $depId) {
    $this->db->select('Students.ID, Students.Full_Name, Students.ID_No, Students.Gender');
    $this->db->from('student as Students');
    
    // 核心过滤条件:仅查询当前部门的学生
    $this->db->where('Students.Dep_ID', $depId);
    
    // 保留搜索功能(如果需要的话)
    if(!empty($searchText)){
        $likeCriteria = "(Students.Full_Name  LIKE '%".$searchText."%'
                        OR Students.ID_No  LIKE '%".$searchText."%')";
        $this->db->where($likeCriteria);
    }
    
    // 处理分页
    if($page !== null && $segment !== null){
        $this->db->limit($page, $segment);
    }
    
    $query = $this->db->get();
    return $query->result();
}

第三步:同步修改分页计数方法

为了保证分页的准确性,userListingCount方法也要加入部门ID过滤:

function userListingCount($searchText = '', $depId) {
    $this->db->select('count(*) as total');
    $this->db->from('student as Students');
    
    // 加入部门过滤
    $this->db->where('Students.Dep_ID', $depId);
    
    if(!empty($searchText)){
        $likeCriteria = "(Students.Full_Name  LIKE '%".$searchText."%'
                        OR Students.ID_No  LIKE '%".$searchText."%')";
        $this->db->where($likeCriteria);
    }
    
    $query = $this->db->get();
    return $query->row()->total;
}

额外注意事项

  • 确保session中确实存储了当前登录用户的Dep_ID,如果你的session存储结构不同,记得调整获取方式;
  • 检查student表的Dep_ID字段类型是varchar,而department表的Dep_ID是int,要保证存储的部门ID数值一致,避免类型不匹配导致查询不到数据;
  • 如果你需要显示部门名称,可以重新关联department表,但过滤条件还是用当前登录用户的Dep_ID即可。

内容的提问来源于stack exchange,提问作者Abush Nasir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:37:27