我想在同一个 Controller 中运行多个模型,但它显示以下错误
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);
}
}
//公司模型
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;
}
}
//产品型号
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;
}
}
最佳答案
您的模型开头为
class Company extends CI_Controller {
他们应该是
class Company extends CI_Model {
<罢工> 尝试改变
$this->load->model('Product');
$this->load->model('Company');
至
$this->load->model('product');
$this->load->model('company');
注意小写的型号名称。刚刚在这里尝试了一下,得到了和你一样的错误。
罢工>
关于php - 多个模型未加载到 codeigniter 中的同一 Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874503/