2
votes

i want to run multiple model in same controller but its show below error

Severity: Notice

Message: Undefined property: Home::$Company

Filename: controllers/Home.php

Line Number: 23

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct()
    {
        parent::__construct ();

        $this->load->model('Product');
        $this->load->model('Company');
        //$this->load->model(array('Product','Company'));


    }
    public function index()
    {

        $Product = $this->Product->getProduct(10);
        if($Product)
        {
            $data['Product'] = $Product;
        }
        $Company = $this->Company->getCompany(10);
        if($Company)
        {
            $data['Company'] = $Company;

        }
        $this->load->view('Home',$data);

    }
}

//Company Model

class Company extends CI_Controller {


    function getCompany($limit=null,$start=0)
    {
        $data = array();
        $query = $this->db->query('SELECT * FROM user as u,category as c,sub_category as sc WHERE c.Category_Id=u._Category_Id and u._Sub_Category_Id=sc.Sub_Category_Id ORDER by rand() limit '.$start.','.$limit);
        $res   = $query->result();        
        return $res;
    }
    function getOneCompany($id)
    {
        $this->db->where('User_Id',$id);
        $query = $this->db->get('user');
        $res = $query->result();
        return $res;
    }
}

// Product model

class Product extends CI_Controller {


    function getProduct($limit=null,$start=0)
    {
        $data = array();
        $query = $this->db->query('SELECT * FROM product as p,user as u,category as c,sub_category as sc WHERE p._User_Id = u.User_Id and  c.Category_Id=u._Category_Id and u._Sub_Category_Id=sc.Sub_Category_Id ORDER by rand() limit '.$start.','.$limit);
        $res   = $query->result();        
        return $res;
    }
    function getOneProduct($id)
    {
        $this->db->where('User_Id',$id);
        $query = $this->db->get('user');
        $res = $query->result();
        return $res;
    }
}
3
Are your Product and Company models extending CI_Model? - Jethro Hazelhurst
Yes Product and Company both are present under model folder. - Hardik Patel
and also both are working without error if i comment any one model load. But when i call Both model then display error. - Hardik Patel
Can you show use the top couple of lines from each of the models please? - Blinkydamo

3 Answers

1
votes

Your models start with

class Company extends CI_Controller {

They should be

class Company extends CI_Model {


Try changing

$this->load->model('Product');
$this->load->model('Company');

to

$this->load->model('product');
$this->load->model('company');

notice the lower case model names. Just tried it here and got the same error as you.

1
votes
$models = array(
    'menu_model' => 'mmodel',
    'user_model' => 'umodel',
    'admin_model' => 'amodel',
);
foreach ($models as $file => $object_name)
{
    $this->load->model($file, $object_name);
}
0
votes

Try giving a name to you model :

$this->load->model('Company', 'Company');