简体   繁体   中英

multiple model not load in same controller in codeigniter

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;
    }
}
$models = array(
    'menu_model' => 'mmodel',
    'user_model' => 'umodel',
    'admin_model' => 'amodel',
);
foreach ($models as $file => $object_name)
{
    $this->load->model($file, $object_name);
}

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.

尝试为您的模型命名:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM