codeigniter模型上的undefined属性错误

时间:2011-04-05 06:11:09

标签: codeigniter

我的模型上出现了未定义的属性错误。我使用火柴盒库进行模块化分离。我的mproduct模型是:

class MProducts extends Model{
    /* function MProducts(){
        $this->load->module_model('cities','MCities');
     }*/

    public $home_city_id ='';

    function __construct(){
        parent::Model();
        $this->home_city_id = $this->MCities->getHomeCityId();
    }
}

我收到此错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: MProducts::$MCities

Filename: models/mproducts.php

Line Number: 12

我的mcities模型是:

class MCities extends Model{

    function MCities(){
        parent::Model();
    }
function getHomeCityId(){
        $city = get_cookie('home_city');
         $this->db->select('id');
         $this->db->where('name', $city);
         $Q = $this->db->get('omc_cities');
         if($Q->num_rows() > 0){
             foreach ($Q->result_array() as $row){
             return $row['id'];
           }
        }
    }

}

我不知道方式是否错误。

编辑 我的产品控制器是这样的:

class Admin extends Shop_Admin_Controller {
    function Admin(){
        parent::Shop_Admin_Controller();
        // Check for access permission
        check('Products');
        // load modules/categories/model/mcats
         $this->load->module_model('categories','MCats');
        // load the MProducts model
        $this->load->model('MProducts');
        // load modules/cities/model/mcities
        $this->load->module_model('cities','MCities');
        // Set breadcrumb
        $this->bep_site->set_crumb($this->lang->line('backendpro_products'),'products/admin');       
    }


    function index(){
        // Setting variables
        $data['title'] = "Manage Products";
        $data['products'] = $this->MProducts->getAllProducts();
        $data['cities'] = $this->MCities->getCitiesDropDown();
        $data['categories'] = $this->MCats->getCategoriesDropDown();
        // we are pulling a header word from language file
        $data['header'] = $this->lang->line('backendpro_access_control');
        $data['page'] = $this->config->item('backendpro_template_admin') . "admin_product_home";
        $data['module'] = 'products';
        $this->load->view($this->_container,$data);
    }
}

1 个答案:

答案 0 :(得分:2)

在你的MProducts的构造函数中,你这样做:$this->MCities->getHomeCityId();这意味着你试图获取MProducts对象的MCities var然后你调用那个对象的getHomeCityId();什么是行不通的因为看在您的MProducts中没有MCities var。

编辑: 您必须在模型中获取codeigniter实例,并从CI实例中检索MCities模型。

MProducts应该是这样的:

class MProducts extends Model{
    /* function MProducts(){
        $this->load->module_model('cities','MCities');
     }*/

    public $home_city_id ='';

    function __construct(){
        parent::Model();
        $CI =& get_instance();

        $this->home_city_id = $CI->MCities->getHomeCityId()->getHomeCityId();
    }
}

我希望这会有所帮助。