4

I am new to codeigniter and i am stuck trying to figure out what is going wrong on this line

here is my controller

class Product extends CI_Controller{

    function index(){
        $this->load->model('product_model');
        $data['products'] = $this->product_model->get_all_products();
        $this->load->view('all_products', $data); 

    }
}

here is my model

class Product_model extends CI_Model {

    function get_all_products(){
        $query = $this->db->get('products');
        if($query->num_rows() > 0){
            foreach($query->result() as $row){
                $data[] = $row;
            } 
            return $data;
        }
    }
}

and here is my error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Product::$db

Filename: core/Model.php

Line Number: 50
Fatal error: Call to a member function get() on a non-object in /Users/matt/Sites/ci/application/models/product_model.php on line 9el.php on line 6

the error is on this line

$query = $this->db->get('products');

why is it failing the codeigniter documentation describes it that way...i have a products table also

hakre
  • 193,403
  • 52
  • 435
  • 836
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • possible duplicate of [codeigniter model error: Undefined property](http://stackoverflow.com/questions/3440839/codeigniter-model-error-undefined-property) – Abdul Manan Jun 05 '15 at 06:47

3 Answers3

12

try

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

in application/config/autoload.php

Ivan
  • 491
  • 6
  • 15
  • You should accept his answer then if it is solved. FYI unless you omitted it for speed above you are missing your constructs. – BrandonS Apr 08 '11 at 20:56
  • @BrandonS: constructors aren't required. – treeface Apr 08 '11 at 21:49
  • @treeface: oh I know and i understand that this may not be his code exactly but if it was i wanted to point out that the use of a constructor is helpful and should be a normal part of your OOP process besides constructors are a efficient way to initialize objects. I just feel that most people in PHP are using selected parts of OOP standards that benefit them and leave out others and in my opinion constructors are a vital part of oop that shouldn't be left out. But you were right in the fact that the use of a construct is not required and i should have worded my comment to reflect that. – BrandonS Apr 08 '11 at 22:12
  • @BrandonS: I'm left wondering a bit what benefit can be gained by an empty constructor, or how it is best OOP practice to have one just for the sake of having one. I'm also a little curious what you mean by "constructors are a efficient way to initialize objects". Sure...if you have something to do in a constructor (like loading a library specific to that controller or model when you don't want to autoload it), but otherwise..? – treeface Apr 09 '11 at 21:36
0

you need to check two things

1- you are not missing constructor

class Product_model extends CI_Model {

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

}

2- you have loaded database library. Go to application/config/autoload.php and add 'database' in autoload libraries.

$autoload['libraries'] = array('database');
waqas
  • 4,357
  • 3
  • 34
  • 42
0

you must go to autoload and replace $autoload['libraries'] with this $autoload['libraries'] = array('database', 'form_validation');

Spudley
  • 166,037
  • 39
  • 233
  • 307
pecabum
  • 27
  • 8