6

我正在关注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不是一个对象。

为什么?我去过一个世界博览会,野餐和牛仔竞技表演,这是我听过的最愚蠢的事情。你确定你有今天的密码?

4

3 回答 3

2

仔细一看,答案很简单:尝试更改此行:

public function __contruct(){

对此:

public function __construct(){

;)

于 2012-09-24T01:34:00.417 回答
0

首先,关于评论:print_r($this);这是一个非常糟糕的主意,因为它只会让你感到困惑。$this几乎不包含 ci 的东西。你正在寻找的是print_r(get_instance());

关于您的问题: ci 小写成员名称。将您的线路更改为此,您就可以开始了:

$this->articles_model->get_article();
于 2012-09-24T01:12:33.757 回答
0

我发现了问题。它在 routes.php 中。这些线

$route['articles/(:any)'] = 'articles/view/$1';
$route['articles'] = 'articles';

我改成这个了...

$route['articles/(:any)'] = 'Articles/view/$1';
$route['articles'] = 'Articles';

现在我得到一个 404,因为我的数据库是空的,正如预期的那样。仍然不知道为什么这些规则...

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1'

从第一个教程开始,没有“页面”被大写。世界永远不会知道...

于 2012-09-24T00:12:25.337 回答