Welcome Guest, Not a member yet? Register   Sign In
"Undefined property" Error when loading a model
#1

[eluser]yNaxon[/eluser]
Hello,

i'm trying to load a model, but when i'm using it I get an error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Games::$Game

Filename: controllers/games.php

Line Number: 13

The Controller (games.php):

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Games extends CI_Controller {

    public function index()
    {    
         show_error($this->lang->line('error_cat_not_found'));
    }
    
    public function cat($id)
    {
        $this->load->model('Game');
        $this->Game->sayhello($id);
    }
}

The model (game.php):

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Game extends CI_Model {

    public function get_cat($id)
    {
        $query = $this->db->query("SELECT `gameid` FROM `games_categories` WHERE `catid`={$id}");
        $result = $query->result_array();
        print_r($result);
    }
    
    public function sayhello($name)
    {
        echo "hello {$name}";
    }
}

I have tried lowercase, uppercase, nothing helps.
#2

[eluser]Mirge[/eluser]
Fresh CI install... I copied your controller & model.. worked fine.

Output: hello 1

URL: http://localhost/games/cat/1/
#3

[eluser]yNaxon[/eluser]
[quote author="Mirge" date="1313181605"]Fresh CI install... I copied your controller & model.. worked fine.

Output: hello 1

URL: http://localhost/games/cat/1/[/quote]
Thanks for answering Smile

I'll start over and find the problem. thanks again Smile
#4

[eluser]Mirge[/eluser]
If you're developing locally & uploading to a remote server (ie: Linux server), make sure your newlines are UNIX newlines & not Windows. Other than that... I'm not sure, it works here Sad

Also make sure that your Game model filename is actually game.php (all lowercase).
#5

[eluser]pickupman[/eluser]
Try using lowercase syntax when loading classes.
Code:
$this->load->model('Game');
$this->Game->sayhello($id);

//to
$this->load->model('game');
$this->game->sayhello($id);
#6

[eluser]Aken[/eluser]
Remember to include the constructor in your model.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Game extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

    public function get_cat($id)
    {
        $query = $this->db->query("SELECT `gameid` FROM `games_categories` WHERE `catid`={$id}");
        $result = $query->result_array();
        print_r($result);
    }
    
    public function sayhello($name)
    {
        echo "hello {$name}";
    }
}
#7

[eluser]Mirge[/eluser]
The parent constructor is called implicitly if the subclass does not define a constructor. You'd only need to explicitly call a parent constructor in the event your child class defines a constructor.
#8

[eluser]Aken[/eluser]
True. I think it's good practice regardless, but you're right it's not necessary in this case.




Theme © iAndrew 2016 - Forum software by © MyBB