【问题标题】:Getting error in loading model in controller in codeigniter Message: Undefined property: Cart::$load在 codeigniter 的控制器中加载模型时出错消息:未定义的属性:Cart::$load
【发布时间】:2013-06-29 12:48:05
【问题描述】:

我是 codeigniter 的初学者,目前我正在研究购物车,并从 http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ 教程中获得帮助。我正在使用 codeigniter 2.1.3。

我收到一个错误:

遇到了 PHP 错误

严重性:通知

消息:未定义的属性:Cart::$load

文件名:controllers/cart.php
行号:7

致命错误:在第 7 行对 D:\xampp\htdocs\ci\application\controllers\cart.php 中的非对象调用成员函数 model()

谁能告诉我为什么它不起作用?

我的控制器的名字是cart.php

<?php
class Cart extends CI_Controller {

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

    public function index()
    {
        $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
        print_r($data['products']);
        //$data['content'] = 'cart/products'; // Select view to display
        //$this->load->view('index', $data); // Display the page
    }
}
?>

我的模型是 cart_model.php

<?php
class Cart_model extends CI_Model{
    //public function _construct(){
        //parent::_construct();
    //}

    public function retive_products(){
        $query = $this->db->get("products");
        return $query->result_array();
    }
}
/* End of file cart_model.php */  
/* Location: ./application/models/cart_model.php */
?>

【问题讨论】:

  • 不要使用 php4 风格的控制器构造函数。如果需要,请使用 __construct() magick 方法并扩展 parent::__construct()
  • 另外,你链接的教程是给CodeIgniter V1.7.2.的,已经很老了

标签: php codeigniter


【解决方案1】:

Codeigniter 2.1.3 旨在支持 PHP 5.2.4 及更高版本。

更改类构造函数:

<?php
  class Cart extends CI_Controller {
    public function __construct()
      {
         parent::__construct();
      }

而不是

public function cart()
    {
        parent::CI_Controller();

【讨论】:

    【解决方案2】:

    尝试检查您的代码。您在模型上调用 retrieve_products 函数,但在模型中您有 retive_products 函数。

    控制器

    public function index() {
          $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
          print_r($data['products']);
    
          //$data['content'] = 'cart/products'; // Select view to display
          //$this->load->view('index', $data); // Display the page
    }
    

    型号

    public function retive_products() {
          $query = $this->db->get("products");
          return $query->result_array();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多