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

线上环境出现Call to a member function send_notification() on null错误求助

排查并解决「Call to a member function send_notification() on null」错误

首先看你遇到的具体错误:

Severity: error --> Exception: Call to a member function send_notification() on null controllers/Admin.php 112

这个错误的核心原因很明确:你在Admin控制器里调用$this->notification_model->send_notification()时,$this->notification_model是null,也就是说模型没有被成功加载。本地运行正常但线上出错,大概率是环境差异(本地Windows不区分文件名大小写,线上Linux区分)或者模型加载步骤缺失导致的。

下面是具体的解决步骤:

1. 手动加载notification_model

CodeIgniter框架不会自动加载所有模型,你需要在使用前手动加载。有两种常用方式:

方式一:在当前方法内加载(仅该方法可用)

直接在notification方法的开头添加模型加载代码:

function notification($param1 = '', $param2 = '') {
    // 加载通知模型
    $this->load->model('notification_model');
    
    // 原有代码保持不变
    if ($param1 == 'create') { 
        $notification_code = $this->crud_model->create_notification(); 
        $check_notification_to_send = $this->input->post('check_notification'); 
        if ($check_notification_to_send == 1) { 
            $data['title'] = $this->input->post('title'); 
            $customers = $this->db->get('customer')->result_array(); 
            $date = date("d M,Y"); 
            $message = $data['title'] . ' '; 
            $message .= get_phrase('for more details please login to your acc'). ' '; 
            $message .= get_phrase('published on') . ' ' . $date; 
            foreach($customers as $row) { 
                $reciever_phone = $row['phone']; 
                $this->notification_model->send_notification($message , $reciever_phone); 
            } 
        } 
    } 
}

方式二:在控制器构造函数中加载(整个控制器通用)

如果Admin控制器的其他方法也需要用到这个模型,可以在构造函数里一次性加载:

class Admin extends CI_Controller {
    public function __construct() {
        parent::__construct();
        // 加载常用模型
        $this->load->model('crud_model');
        $this->load->model('notification_model');
    }

    // 你的notification方法...
}

2. 检查模型文件的命名规范(重点排查线上环境)

线上服务器通常是Linux系统,文件名严格区分大小写,而本地Windows系统不区分,这是很多“本地正常线上报错”的根源:

  • 模型文件名必须是首字母大写的格式,比如Notification_model.php
  • 模型类名必须和文件名完全一致,示例:
class Notification_model extends CI_Model {
    // 你的send_notification方法...
}

如果你的模型文件名是全小写的notification_model.php,本地Windows能正常加载,但线上Linux会找不到文件,导致模型实例为null。

3. 确认模型文件路径正确

确保notification_model.php放在application/models/目录下,如果放在子目录(比如application/models/notifications/),加载时需要带上路径:

$this->load->model('notifications/notification_model');

按照上面的步骤修改后,应该就能解决这个错误了。

内容的提问来源于stack exchange,提问作者AbuuJurayj Swabur Buruhan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:36:54