duan010167787 2012-09-13 20:28
浏览 41
已采纳

在codeigniter中获取“在非对象上调用成员函数函数()”错误[复制]

This question already has an answer here:

I'm very new to CodeIgniter and not an expert in OOP so please bear with me.

This is the function I have in my model:

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

This is in my controller that is calling that model:

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

An in my view:

$company = array(
        'name'     => 'company',
        'id'          => 'company',
        'value'       => "$company->name",
        'class'       => 'styl_f validate[required] text-input input-xlarge',
        'placeholder' => "$company->name"
);

echo $company['value']

The error I am getting is this: Call to a member function get_company() on a non-object in C:\..\application\modules\accounts\controllers\accounts.php I am under the impression that I am receiving these errors because I am passing a non object through get_company() but the thing that confuses me is that this error does not come up for get_profile(); The get_profile() function in my model is very similiar to my get_company() function. What is causing this error? How can I get rid of it?

</div>
  • 写回答

3条回答 默认 最新

  • doukang7858 2012-09-13 20:33
    关注

    The problem is within your controller:

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

    The $profile variable uses $this->profiles_m as the object, but $company misses the letter 's' in the object.

    Try with this line instead:

        $company = $this->profiles_m->get_company($this->access->getUid());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c