加载模型时的CodeIgniter Undefined属性 [英] CodeIgniter Undefined property when loading a model
问题描述
在尝试在codeigniter中实现非常简单的模型控制器时,我收到以下错误消息。我是该框架的新手,但据我所知,这应该可行。
I'm receiving the following errors whilst trying to implement a very simple model controller in codeigniter. I'm new to the framework but as far as I can see this should work.
我也尝试过自动加载模型。我正在自动加载数据库库。
I've also tried auto loading the model. I am autoloading the database library.
Message: Undefined property: User::$user_model
Fatal error: Call to a member function get_user() on a non-object
模型上调用成员函数get_user()
The model
class User_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get_user()
{
return "test";
}
}
控制器
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function index()
{
$this->load->model('User_model');
$data['value'] = $this->User_model->get_user();
$this->load->view('user_edit', $data);
}
}
谢谢
推荐答案
我认为我已经找到问题了。我的用户控制器中有很多其他的setter方法,这些方法我一直使用,并不认为这会成为问题。
I think i've found the problem. I had some additional setter methods in my user controller which i've used all the time, didnt think they would be a problem.
public function __set($key, $value)
{
// Check to see that the requested attribute exists and then assign the value
if (property_exists($this, $key))
{
$this->$key = $value;
}
}
原来我需要带下划线之一
turns out I needed to take away one of the underscores as codeigniter doesn't like it.
public function _set($key, $value)
我本应该真正包括完整的课程,但是我试图使其尽可能简单!
I should have really included the full class but I was trying to keep it as simple as possible!
这篇关于加载模型时的CodeIgniter Undefined属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!