CodeIgniter - 模型已加载但无法使用?
好的。所以我正在使用 CI 开发一个网站。这是我的控制器的结构:
class MY_Controller extends Controller
class User extends MY_Controller
class User_Model
因此,我将 User_Model 加载到 User 控制器的构造函数中。我知道它已正确加载,因为我尝试从 User_Model 打印一些内容并且它工作得很好。但是,当我使用用户控制器中的 User_Model 中的函数之一时,它开始出现错误。这是我收到的错误:
未定义的属性:User::$User_Model
有人有什么想法吗?
这是扩展控制器
class MY_Controller extends Controller {
public function __construct() {
parent::Controller();
}
}
这是控制器
class User extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
echo $this->user_model->validate_user('hartantothio');
}
}
这是 User_model
class User_model extends Model {
public function __construct() {
parent::Model();
}
public function validate_user($user, $pass = '') {
return '123';
}
}
Ok. So I am working on a website using CI. Here is the structure of my controller:
class MY_Controller extends Controller
class User extends MY_Controller
class User_Model
So, I load the User_Model inside the constructor of the User controller. I know that it is loaded properly because I tried to print something from User_Model and it worked just fine. However, when I use one of the functions in the User_Model from User controller, it started giving me the error. This is the error I received:
Undefined property: User::$User_Model
Anybody has any idea?
This is extended controller
class MY_Controller extends Controller {
public function __construct() {
parent::Controller();
}
}
This is the controller
class User extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
echo $this->user_model->validate_user('hartantothio');
}
}
This is the User_model
class User_model extends Model {
public function __construct() {
parent::Model();
}
public function validate_user($user, $pass = '') {
return '123';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该阅读
模型名称区分大小写!
should read
Models names are case-sensitive!
您是否使用语法 $this->User_Model->function_name() 来调用该函数?
我还知道我过去也遇到过区分大小写的问题。
Are you calling the function using the syntax, $this->User_Model->function_name()?
I also know that I've run into problems with case sensitivity as well in the past.
好吧,通过使用 MY_Controller 并直接扩展控制器的用户控制器,可以解决这个问题。
Well, by taking the MY_Controller and extends the User controller directly of the Controller, that fixes the issue.
你把
My_Controller
文件放在哪里?我把我的放在system/application/libraries
中,没有任何问题。另外,我使用 PHP4 构造函数方式来编写它,而不是__constructor
:Where do you put
My_Controller
file? I put mine insystem/application/libraries
and don't have any problems with it. Also, I use PHP4 constructor way to write it, instead of__constructor
: