2012-07-03 80 views
0

我得到的错误错误调用成员函数

Fatal error: Call to a member function retrieve_products() on a non-object 

控制器是:

<?php 
class Cart extends CI_Controller { // Our Cart class extends the Controller class 

    public function _construct() 
    { 
     parent::_construct(); // We define the the Controller class is the parent. 
     $this->load->model('Cart_model'); // Load our cart model for our entire class 
    } 

    function index() 
    { 
     $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products 
    } 
} 

的模型是:

<?php 
class Cart_model extends CI_Model { 

    function retrieve_products(){ 
     $query = $this->db->get('products'); // Select the table products 
     return $query->result_array(); // Return the results in a array. 
    }    
} 

回答

1

我想说的是,你的电话

$data['products'] = $this->cart_model->retrieve_products(); 

应该是:

$data['products'] = $this->Cart_model->retrieve_products(); 

即:在cart_model大写字母“C”

+0

好吧,已经试过,但显示相同的问题。在某处,我发现当数据库表(在这种情况下'产品')中没有条目时,它显示错误,但我确实有一些条目。 –

+0

数据库库是否存在并加载? – jco

1

也许我们'使用不同的版本(我有1.7.2),但要声明一个模型,CI_不会出现。我的工作代码具有相当于:

class Cart_model extends Model 

此外,类应该大写:

$this->Cart_model->retrieve_products(); 

(代替)

$this->cart_model->retrieve_products(); 
0

我觉得这是你的错字错误你有拼写构造函数作为_construct而非__construct这就是为什么笨认为它作为一种功能,而不是一类的构造函数和模型加载仅限于该功能。

相关问题