Smandoli
Smandoli

Reputation: 7019

CodeIgniter -- unable to use an object

THE SUMMARY:

When I call .../index.php/product, I receive:

Fatal error: Call to a member function get_prod_single() on a non-object in /var/www/sparts/main/controllers/product.php on line 16

The offending Line 16 is:

$data['pn_oem'] = $this->product_model->get_prod_single($product_id);

Looks like I don't know how to make this a working object. Can you help me?

THE CODE:

In my /Models folder I have product_model.php:

<?php
class Product_model extends Model {

    function Product_model()
    {
        parent::Model();
    }

    function get_prod_single($product_id)
    {
        //This will be a DB lookup ...
        return 'foo';  //stub to get going
    }   
}
?>

In my /controllers folder I have product.php:

<?php
class Product extends Controller {

    function Product()
    {
        parent::Controller();
    }

    function index()  {

      $this->load->model('Product_model');

      $product_id = 113; // will get this dynamically

      $data['product_id'] = $product_id;
      $data['pn_oem'] = $this->product_model->get_prod_single($product_id);

      $this->load->view('prod_single', $data);
    }
}
?>

Upvotes: 1

Views: 171

Answers (3)

yretuta
yretuta

Reputation: 8091

CI's model names are case sensitive, so "product_model" instead of "Product_model" will do the job. If you want to, you can set some sort of an alias, If I remember correctly, it goes something like

$this->load->model('product_model', 'Product_model');

that would have also worked.

EDIT: slight typo in the alias. fixed

Upvotes: 1

Favio
Favio

Reputation: 1009

Just change:

$this->load->model('Product_model');

to:

$this->load->model('product_model');

It seems like the name passed to the loader references the file name.

Upvotes: 3

ronaldosantana
ronaldosantana

Reputation: 5252

I think this $this->load->model('Product_model'); will create a model with name Product_model and not product_model... PHP is case sensitive.

Upvotes: 1

Related Questions