Welcome Guest, Not a member yet? Register   Sign In
Models aren't loading - why is that?
#1

[eluser]yarek[/eluser]
Hello. I know, I know... it was discussed so many times, I searched the forums but nothing helped. Problem: "Call to a member function on a non-object in C:\http\cms\system\application\models\entry.php on line 17"

Here is my code:

cms.php
Code:
<?php
    class Cms extends Controller {
        
        function Cms() {
            parent::Controller();
            
            $this->load->model('Entry');
        }
    
        function index() {                
            $data['entries'] = $this->Entry->getAllEntries();
        }
        
        function entry($id) {            
            $data['entry'] = $this->Entry->getEntry($id);
        }
                
    }    
?>

entry.php
Code:
<?php
    class Entry extends Model {
        
        function Entry() {
            parent::Model();
        }
    
        function getEntry($id) {
            $this->db->where('id', $id);
            
            $query = $this->db->get('entries');
            
            return $query->result();
        }
        
        function getAllEntries() {
            $this->db->orderby('modified', 'desc');
            
            $query = $this->db->get('entries');
            
            return $query->result();            
        }    
    }
?>

I tried everything, I changed all names to lowercase etc and it didn't work. I tried model('', TRUE); and it didn't help me too Sad

And what is interesting: it sees problem on the 17th line! There is a similliar function on 11th line and it doesn't see that. Strange?
#2

[eluser]xwero[/eluser]
have you autoloaded the database else you have to add
Code:
$this->load->database()
to your controller.
#3

[eluser]nirbhab[/eluser]
hi,

i am not sure, but the way i use model is:
Code:
$this->load->model('Entry','Entry',TRUE);

Please try this and mention the error if possible.
#4

[eluser]Dr.Dan[/eluser]
Code:
$this->db->orderby('modified', 'desc');

Make sure you have a column 'modified', and it is correctly spelled.
#5

[eluser]andreagam[/eluser]
Yarek, I think you should call the model like this:
Code:
$this->load->model('entry');

That is, the name of the file in lower case.

Try that

Andrea
#6

[eluser]yarek[/eluser]
@andrea, Lowercase or uppercase - it doesn't work, I already tried that.

@Dr. Dan, the name of column is correct, it's not the problem of database because there would occut another error message, right? I think so... I checked it.
#7

[eluser]tonanbarbarian[/eluser]
is active record turned on
config/database.php
Code:
$db['default']['active_r'] = TRUE;
#8

[eluser]yarek[/eluser]
[quote author="tonanbarbarian" date="1199929438"]is active record turned on
config/database.php
Code:
$db['default']['active_r'] = TRUE;
[/quote]

Yes, it's and it was turned on Sad

Why do you think that the problem is in the database? I rather think that it's something with loading models, they just can't load.
#9

[eluser]yarek[/eluser]
[quote author="xwero" date="1199848845"]have you autoloaded the database else you have to add
Code:
$this->load->database()
to your controller.[/quote]

Omg, I feel so ashamed... I didn't load this.

The problem is resolved - I loaded database class in the controller's contructor. It works!

Thank you all for replies. I didn't know about loading database class because I didn't know I have to do that. Victory!




Theme © iAndrew 2016 - Forum software by © MyBB