Welcome Guest, Not a member yet? Register   Sign In
New to CI - stuck on news tutorial with an 'Undefined property' error
#1

[eluser]jimbojames[/eluser]
Hi All,

I am new to CI coming from a .Net background.

I have been following the tutorial and all was going well until I got to the end of the news tutorial section.

I am getting the following error when I try and visit my index.php/news page:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: News::$news_model

Filename: controllers/news.php

Line Number: 13

I have checked my code line by line and can't see any differences between the tutorial and my versions.

I have set up logging in MySQL and I can see that CI is hitting the DB and registering the following entry in to the logs:

Code:
Init DB root
Query SET NAMES utf8

I have also checked to see if all my cases are correct, which they seem to be.

I have now been trying to fix this error for 8 hours and I'm out of ideas.

Any help would be greatly received!

FYI (if it helps diagnose the issue), I am running a Mac on Mountain Lion 10.8.3 with the latest CI, MySQL and PHP installations.

Thanks in advance,

James
#2

[eluser]jimbojames[/eluser]
Anyone? Really thought I would have a reply by now!
#3

[eluser]TheFuzzy0ne[/eluser]
Patience is a virtue. Don't forget, the people who help others on these forums are volunteers -- we don't get paid for it. Sometimes it can take a while to get a reply. I've just finished replying to some posts that have been waiting over a week. Generally, replies are quite fast (usually within a few hours), but often people are busy with real-life responsibilities and posts can be overlooked.

Anyway... Welcome to the CodeIgniter forums!

First of all, it would help a lot if you posted your controller. It sounds to me as if you are not loading your model.

#4

[eluser]jimbojames[/eluser]
Hi TheFuzzyOne,

I'm sorry, maybe I was a bit hasty, but I was looking forward to learning a new framework and to then get an error on the tutorial, spend hours trying to resolve it with Google, finally throw the towel in and post in these forums and then no reply (at first), I was a bit disappointed.

Anyway, my controller code is the same at the tutorial from the CI site:

Code:
<?php

class News extends CI_Controller {
    
    public function __contruct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }
    
    public function index()
    {  
        $data['news'] = $this->news_model->gets_news();
        $data['title'] = 'News archive';
        
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }
    
    public function view($slug)
    {
        $data['news'] = $this->news_model->gets_news($slug);
        
        if(empty($data['news_item']))
        {
            show_404();
        }
        
        $data['title'] = $data['news_item']['title'];
        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }
    
}

Can you see any errors?

I have since read that it looks as if Ellis Lab is going cold on CI and there is only 1 guy contributing at the moment. What's your thoughts?
#5

[eluser]TheFuzzy0ne[/eluser]
Sorry, which tutorial is it that you're following? The one in the user guide? The reason I ask is that what you've posted looks nothing like what in the user guide tutorial. However, I also see no reason why it shouldn't be working.

Have you modified any system files?

I don't think Ellislab is going cold on CI, but it certainly seems that it's not a priority for them anymore (perhaps this is because it's not profitable enough?), and that they don't want it to get too far ahead of itself. As it stands, CodeIgniter is a great framework, and it has some of the best documentation I've ever seen. However, CodeIgniter seems to be lacking direction and leadership. I'm hoping that CodeIgniter will eventually pick up the slack again, but I guess we'll just have to wait and see. In order for CodeIgniter to utilise some of the new PHP features, it would involve a complete rewrite, which I don't think Rick wants.

With that said, CodeIgniter is still very much alive, and community-driven, and there's definitely more than one contributor. Wink

https://github.com/EllisLab/CodeIgniter/
#6

[eluser]phpLearner[/eluser]
change $data['news'] to $data['news_item'] in function view($slug){ }
#7

[eluser]jimbojames[/eluser]
FuzzyOne:

It's the tutorial here

It seems like a really good framework to learn - just just need to get it working :-(

phpLearner:

I thought you had found the issue but I still get an error. Do you have any other ideas?
#8

[eluser]TheFuzzy0ne[/eluser]
Please make sure error reporting is enabled. At the beginning of index.php:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ini_set('display_errors', '1');
error_reporting(E_ALL);

Also, double-check that you don't have any errors displaying before the one you posted. Sometimes one error snowballs and causes more errors, and fixing the first one fixes subsequent errors.

Please also post the contents of ./application/models/news_model.php
#9

[eluser]jimbojames[/eluser]
I added the code as you said but all I get displayed, no matter what url I visit in the app is 'no direct script allowed'

I looked at the index.php file and it has this section already in it:

Code:
define('ENVIRONMENT', 'development');


if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
  case 'development':
   error_reporting(E_ALL);
  break;

  case 'testing':
  case 'production':
   error_reporting(0);
  break;

  default:
   exit('The application environment is not set correctly.');
}
}

So am I right to think it's already in the right reporting mode?

The extra code you required is:

Code:
<?php

class News_model extends CI_Model {
    
    public function __construct()
    {
        $this->load->database();
    }
    
    public function get_news($slug = FALSE)
    {
        if ($slug === FALSE)
        {
           $query = $this->db->get('news');
           return $query->result_array();
        }                    
    
    $query = $this->db->get_where('news', array('slug' => $slug));
    return $query->row_array();
    
    }
}

Thanks for your help with this, much appreciated!
#10

[eluser]TheFuzzy0ne[/eluser]
By default, error reporting should be enabled in your php.ini, but I find that sometimes it's not, so I like to explicitly call ini_set('display_errors', '1') just to be sure.

I'm not sure if/how it'll fix your problem, but you're not calling the parent constructor from your model:
Code:
class News_model extends CI_Model {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }




Theme © iAndrew 2016 - Forum software by © MyBB