0

I am new to Codeigniter and the version I am using is the latest Codeigniter v2.1.4.

I am doing some simple CRUD as a start for making my own web blog but it's getting an error message on my controller as following.

Message: Undefined property: Site::$site_model

Controller

function blog() {
        $data = array();
        $query = $this->site_model->get_records();

        if (isset($query)) {
            $data['records'] = $query;
        }

        $data['main_content'] = 'blog';
        $this->load->view('includes/template', $data);
    }

It's complaining on this line $query = $this->site_model->get_records();

Model

function get_records() {
        $query = $this->db->get('data');
        return $query->result();
    }

db library is loaded as well..

$autoload['libraries'] = array('database');

What am I doing wrong?

1
  • where did you load your model? Commented Jul 23, 2013 at 11:27

1 Answer 1

1

Before that you need to load the model like

$data = array();
$this->load->model('site_model');   //Here
$query = $this->site_model->get_records();

When ever you are dealing with the model functions makesure that before that you need to load the model as well.

As Hashem Qolami said you can auto load the model like

$autoload['model'] = array('model1', 'model2'); 

But in my opinion auto loading of all the models may create some performance issues(Iam not sure) but you can do this.

Sign up to request clarification or add additional context in comments.

7 Comments

Also you can load essential models by: $autoload['model'] = array('model1', 'model2');
yes you are right Hashem Qolami but I think its not the beter option to load each model using autoload.
That was super fast! The tutorial I followed is for the old version and this is probably one of things that changed. Thanks very much.
@Gautam3164 Absolutely, I said essential models not each one ;)
@SeongLee Believe me, there's no sensible different between 2.1.3 and 2.1.4, just some XSS filtering improvement ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.