1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You are trying to get to access the MCities inside a Model but Model is separated from Controler. That's why you have to do it in the controller and not inside of the Model.
i have no idea where to put in controller. please can u tell me in detail or an example.
actually i don't konw how your controller looks like.
Ahhh ok now i see the problem. I've changed my answer. this should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.