【问题标题】:Why ERROR in Codeigniter为什么 Codeigniter 出现错误
【发布时间】:2015-10-24 00:54:59
【问题描述】:

我有错误,请帮助

遇到了 PHP 错误

严重性:通知

消息:未定义的属性:用户::$user_model

文件名:controllers/user.php

行号:15

回溯:

文件:C:\wamp\www\apcodeigniter\application\controllers\user.php 行: 15 函数:_error_handler

文件:C:\wamp\www\apcodeigniter\index.php 行:292 功能: 需要一次

第 15 行是:public function get()

public function get()
{
    $this->user_model->get();
}

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    你还没有加载模型。

    尝试将User控制器中的get方法更改为-

    public function get()
    {
        $this->load->model('user_model');
        $this->user_model->get();
    }
    

    我通常在控制器中做什么,这将取决于该模型,并且每种方法都需要该模型的某些方法。

    /*
     * Load the model in the controller's constructor
     */
    class User extends CI_Controller
    {
        function __construct()
        {
            parent::_construct(); //This is important if you're adding a constructor in the Controller, so that it can properly initialize the scope of CI_Controller in itselves
            $this->load->model(array('user_model'));
        }
    
        public function get() //public here really isn't necessary, but kept it for understanding
        {
            $this->user_model->get();
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • @Jana 如果此答案对您有帮助,请接受。
    • Hej Nitesh 请再问一个问题.. 为什么它删除表中的所有行它应该只删除 1row 这里是模块中的 cod: public function delete($user_id) $this->db->delete ('用户', ['user_id => $user_id']);返回 $this->db->affected_rows();在控制器中是: public function delete($user_id) $this->load->model('user_model'); $result = $this->user_model->delete($user_id);
    • "Quotes" 在您在模型中的删除方法中提供的数组中应该是 ['user_id' => $user_id] 而不是 ['user_id => $user_id']
    • 另一个快速提示@Jana,你应该在另一个问题而不是评论中问它。
    • 好的,非常感谢您的快速回复!最好的问候:)
    猜你喜欢
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多