Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Undefined Property, or how will my controller stop worrying and learn to love the model

I am following the tutorial from the Codeigniter website about making a news section. Right now I am getting an undefined property message on line 13.

Here is my model. applications/models/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();
}

And here is the controller. applications/controllers/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');
    }
}

These are all the routes I have setup. applications/config/routes.php

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

My database looks like this

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    |                |
+-------+--------------+------+-----+---------+----------------+

I have tried using the property with a capital first-letter,$this->Articles_model, and not, $this->articles_model.

Am I missing something stupid? If so what is it?
If it's not stupid how can I debug?

EDIT

The ouput of print_r, as per comments, did not contain "Articles_model". The first tier thing is over my head, but CTRL-F isn't. Also the Apache logs mention this...

[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

EDIT1

I thought I fixed it, but no, see the comment on my answer. For some reason $this->article_model is not an object.

Why? I've been to one World Fair, a Picnic and a Rodeo and that's the stupidest thing I ever heard come over a set of earphones. You sure you got today's codes?

like image 852
noel Avatar asked Sep 23 '12 22:09

noel


1 Answers

Had a closer look and the answer is very simple: Try changing this line:

public function __contruct(){

to this:

public function __construct(){

;)

like image 146
Chronial Avatar answered Oct 06 '22 22:10

Chronial