codeigniter 如何在ci3中从hooks文件加载模型[duplicate]

dohp0rv5  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(126)

此问题在此处已有答案

How do I load my models inside a codeigniter hook file(2个答案)
两个月前关门了。
我正在学习如何在CI3中使用hooks,是否可以从CI3中的hooks文件加载模型?因为当我尝试加载模型时,它给我这个错误:

A PHP Error was encountered
Severity: Warning

Message: Undefined property: Http_request_logger::$load

Filename: hooks/http_request_logger.php

Line Number: 9

还有

An uncaught Exception was encountered
Type: Error

Message: Call to a member function model() on null

Filename: D:\xampp\htdocs\vobot-cms\application\hooks\http_request_logger.php

Line Number: 9

这是我的hooks代码:

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Http_request_logger {

    public function activity_user() {
        $CI = & get_instance();
        $this->load->model('./activity_user/MActivityUser');

       
        // log_message('info', 'GET --> ' . var_export($CI->input->get(null), true));
        $post = log_message('info', 'POST --> ' . var_export($CI->input->post(null), true));                
        // log_message('info', '$_SERVER -->' . var_export($_SERVER, true));

        $this->MActivityUser->insertLogActivity( $data = array(  
            'username' => (!$this->MActivityUser->getUsername() ? NULL : $this->MActivityUser->getUsername()),
            // 'domain' => $_SERVER["SERVER_NAME"],
            'menu_name' => $_SERVER["REQUEST_URI"],
            'activity' => $post,
            // 'ip' => $this->CI->input->ip_address(),
            'create_date' => date('Y-m-d H:i:s'),
            'cms_id' => $this->MActivityUser->getUserId()
    ));
    return $data;
    }

}
unhi4e5o

unhi4e5o1#

错误是您正在将CI示例加载到$CI变量中,但没有使用它来加载模型。

$CI = & get_instance();
$this->load->model('./activity_user/MActivityUser');

应该是

$CI = & get_instance();
$CI->load->model('./activity_user/MActivityUser');

相关问题