我是CodeIgniter的新手,不是OOP的专家,所以请耐心等待。
这是我在模型中的功能:
function get_company(int $user_id, $fields = '*'){
$r = $this->db->query("SELECT $fields FROM ".$this->db->dbprefix('companies')." WHERE user_id=?", $user_id)->row();
return $r;
}
function get_profile($user_id, $fields = '*'){
$r = $this->db->query("SELECT $fields FROM ".$this->db->dbprefix('users_profiles')." WHERE user_id=?", $user_id)->row();
return $r;
}
这是在我的控制器中调用该模型:
function index(){
$this->load->model('profiles_m');
$profile = $this->profiles_m->get_profile($this->access->getUid());
$company = $this->profile_m->get_company($this->access->getUid());
$vars = array(
'profile'=>$profile,
'company'=>$company,
);
$this->_getTemplate()->build('account', $vars);
}
在我看来:
$company = array(
'name' => 'company',
'id' => 'company',
'value' => "$company->name",
'class' => 'styl_f validate[required] text-input input-xlarge',
'placeholder' => "$company->name"
);
echo $company['value']
我得到的错误是:Call to a member function get_company() on a non-object in C:\..\application\modules\accounts\controllers\accounts.php
我的印象是我收到这些错误,因为我通过get_company()传递一个非对象,但令我困惑的是get_profile();我的模型中的get_profile()函数与我的get_company()函数非常相似。导致此错误的原因是什么?我怎么能摆脱它?
答案 0 :(得分:2)
问题出在您的控制器内:
function index(){
$this->load->model('profiles_m');
$profile = $this->profiles_m->get_profile($this->access->getUid());
$company = $this->profile_m->get_company($this->access->getUid()); // Right here
$vars = array(
'profile'=>$profile,
'company'=>$company,
);
$this->_getTemplate()->build('account', $vars);
}
$ profile变量使用$this->profiles_m
作为对象,但$ company错过了对象中的字母's'。
请尝试使用此行:
$company = $this->profiles_m->get_company($this->access->getUid());
答案 1 :(得分:1)
你有一个拼写错误,该行应为:
$company = $this->profiles_m->get_company($this->access->getUid());
注意'profiles_m'而不是'profile_m'。
答案 2 :(得分:1)
$company = $this->profile_m->get_company($this->access->getUid());
将'profile_m'替换为'profiles_m'