我正在关注Codeigniter 网站上关于制作新闻部分的教程。现在我在第 13 行收到一条未定义的属性消息。
这是我的模型。应用程序/模型/articles_model
<?php
class Articles_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_article($slug = False){
if ($slug === FALSE){
$query = $this->db->get('articles');
return $query->result_array();
}
$query = $this->db->get('articles');
return $query->result_array();
}
$query = $this->db->get_where('articles', array('slug' => $slug));
return $query->row_array();
}
这是控制器。应用程序/控制器/articles.php
<?php
class Articles extends CI_Controller{
public function __contruct(){
parent::__construct();
$this->load->model('Articles_model');
}
public function index(){
//Message: Undefined property Articles::Articles_model
$data['articles'] = $this->Articles_model->get_article();
$data['title'] = "Archive";
$this->load->view('templates/header', $data);
$this->load->view('articles/index', $data);
$this->load->view('templates/footer');
}
public function view($slug){
$data['articles'] = $this->Articles_model->get_article($slug);
if(empty($data['articles'])){
show_404();
}
$data['title'] = $data['articles']['title'];
$this->load->view('templates/header', $data);
$this->load->view('articles/view', $data);
$this->load->view('templates/footer');
}
}
这些是我设置的所有路线。应用程序/config/routes.php
$route['articles/(:any)'] = 'articles/view/$1';
$route['articles'] = 'articles';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
我的数据库看起来像这样
mysql> describe articles
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(128) | NO | | NULL | |
| slug | varchar(128) | NO | MUL | NULL | |
| text | text | NO | | NULL | |
+-------+--------------+------+-----+---------+----------------+
我尝试过使用首字母大写的属性$this->Articles_model
,而不是,$this->articles_model
。
我错过了什么愚蠢的东西吗?如果是这样,那是什么?
如果它不愚蠢,我该如何调试?
编辑
根据评论,print_r 的输出不包含“Articles_model”。第一层的事情在我头上,但 CTRL-F 不是。Apache日志也提到了这一点......
[Sun Sep 23 13:19:30 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function get_article() on a non-object in /home/noel/dev/web/
ci/CodeIgniter_2.1.2/application/controllers/articles.php on line 13
编辑1
我以为我修好了,但没有,请参阅我的答案的评论。由于某种原因$this->article_model
不是一个对象。
为什么?我去过一个世界博览会,野餐和牛仔竞技表演,这是我听过的最愚蠢的事情。你确定你有今天的密码?