codeigniter undefined属性错误模型 [英] codeigniter undefined property error on model

查看:147
本文介绍了codeigniter undefined属性错误模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模型中得到undefined属性错误。我使用火柴盒库模块化分离。我的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();
}
}

我得到这个错误:

 遇到PHP错误

严重性:注意

消息:未定义属性:MProducts: :$ MCities

文件名:models / mproducts.php

行号:12

我的mcities模型是:

  class MCities extends Model {

function MCities(){
parent :: Model();
}
function getHomeCityId(){
$ city = get_cookie('home_city');
$ this-> db-> select('id');
$ this-> db->其中('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('Products');
// load modules / categories / model / mcats
$ this-> load-> module_model('categories','MCats');
//加载MProducts模型
$ this-> load-> model('MProducts');
// load modules / cities / model / mcities
$ this-> load-> module_model('cities','MCities');
//设置面包屑
$ this-> bep_site-> set_crumb($ this-> lang-> line('backendpro_products'),'products / admin');
}


函数索引(){
//设置变量
$ data ['title'] =管理产品;
$ data ['products'] = $ this-> MProducts-> getAllProducts();
$ data ['cities'] = $ this-> MCities-> getCitiesDropDown();
$ data ['categories'] = $ this-> MCats-> getCategoriesDropDown();
//我们从语言文件中拉出一个标题字
$ 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);
}
}


解决方案

你的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();
}
}

我希望这有助。


i am getting undefined property error on my model. I m using matchbox library for modular separation. my mproduct model is:

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();
    }
}

and i get this error :

A PHP Error was encountered

Severity: Notice

Message: Undefined property: MProducts::$MCities

Filename: models/mproducts.php

Line Number: 12

my mcities model is:

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'];
           }
        }
    }

}

i don't know either the way is wrong.

edited my product controller is like this:

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);
    }
}

解决方案

In your constructor of MProducts you do this: $this->MCities->getHomeCityId(); this means you try to get the MCities var of the MProducts Object and then you call the getHomeCityId(); of that object what won't work because looking at your MProducts there is no MCities var.

EDIT: You have to get a codeigniter instance inside your model and retreive the MCities model from the CI instance.

MProducts should be like this:

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();
    }
}

I hope this helps.

这篇关于codeigniter undefined属性错误模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆