EllisLab text mark
Advanced Search
     
New to CI - stuck on news tutorial with an ‘Undefined property’ error
Posted: 18 May 2013 06:24 PM
Joined: 2013-05-18
7 posts

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:

A PHP Error was encountered

Severity
Notice

Message
Undefined propertyNews::$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:

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

 

 
Posted: 19 May 2013 08:47 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2013-05-18
7 posts

Anyone? Really thought I would have a reply by now!

 
Posted: 19 May 2013 10:21 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

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.

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 19 May 2013 02:36 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2013-05-18
7 posts

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:

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

 
Posted: 20 May 2013 05:31 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

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/

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 20 May 2013 06:25 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2013-03-27
6 posts

change $data[‘news’] to $data[‘news_item’] in function view($slug){ }

 
Posted: 20 May 2013 02:12 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2013-05-18
7 posts

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?

 
Posted: 21 May 2013 01:57 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

Please make sure error reporting is enabled. At the beginning of index.php:

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

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 21 May 2013 04:15 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2013-05-18
7 posts

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:

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:

<?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!

 
Posted: 22 May 2013 06:05 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

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:

class News_model extends CI_Model {
    
    
public function __construct()
    
{
        parent
::__construct();
        
$this->load->database();
    
 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 25 May 2013 03:13 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2013-05-18
7 posts

Hi Fuzzy,

I tried your suggestion and still no joy.

Do you have a demo that you know works that I download and try and run on my machine.

Could this be a config error on my mac?

 
Posted: 28 May 2013 07:55 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

Please can you zip up your application directory and upload it to the forums. It would probably make it much easier for me to figure out what’s going on. It’s probably something really simple.

Also, I dealt with a similar problem to this the other day. It turns out that the guy was using Netbeans, and had a file full of variable declarations to make his code-suggestions work. Removing the file mysteriously fixed the problem. Are you using Netbeans?

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 29 May 2013 07:12 AM   [ # 12 ]   [ Rating: 0 ]
Joined: 2013-05-18
7 posts

Hi Fuzzy,

Yes I am using Netbeans :-(

I have removed what I think are the Netbeans files and re-ran and I’m getting no errors ... but no data either?!?!?

I am going to redo the tutorial and try again and I’ll let you know how I get on.

Thanks for your help with this - I will get it to work if its the last thing I do!