Search code examples
codeigniter

codeigniter undefined property error on model


I am getting an 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 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);
    }
}

Solution

  • 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.