noe*_*oel 6 php oop codeigniter
我正在遵循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();
}
Run Code Online (Sandbox Code Playgroud)
这是控制器.应用/控制器/ 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');
}
}
Run Code Online (Sandbox Code Playgroud)
这些是我设置的所有路线.应用程序/配置/ routes.php文件
$route['articles/(:any)'] = 'articles/view/$1';
$route['articles'] = 'articles';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
Run Code Online (Sandbox Code Playgroud)
我的数据库看起来像这样
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 | |
+-------+--------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)
我尝试过使用首字母大写的属性$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
Run Code Online (Sandbox Code Playgroud)
EDIT1
我以为我修好了,但不,看看我的回答评论.由于某种原因$this->article_model不是一个对象.
为什么?我去过一个世界博览会,一个野餐和一个牛仔竞技表演,这是我听过的最愚蠢的事情.你确定你有今天的代码吗?
仔细一看,答案很简单:尝试更改这一行:
public function __contruct(){
Run Code Online (Sandbox Code Playgroud)
对此:
public function __construct(){
Run Code Online (Sandbox Code Playgroud)
;)
| 归档时间: |
|
| 查看次数: |
2522 次 |
| 最近记录: |