使用活动记录在编码器点火器中显示数据


Using active records to display data in codeigniter

所以我相信我说得对(我是活动记录的新手)。我遇到的问题不断出现错误

Message: Call to a member function getbank() on a non-object

我的代码很简单

控制器

class Profile extends CI_Controller
{
    function index()
    {
        $data = array();
        if($query = $this->profile_model->getbank())
        {
            $data['records'] = $query;
        }
        $this->load->view('profile_view', $data);
    }
}

   class Profile_model extends CI_Model {
    function getbank()
{
    $query = $this->db->get('bank');
    return $query->results();
}

它是如此简单,我看不到错误,谢谢。

试试这个

控制器

class Profile extends CI_Controller
{
    function index()
    {
        $data = array();
        $this->load->model('profile_model'); # Added
        $query = $this->profile_model->getbank(); # Changed
        if(!empty($query)) # Changed
        {
            $data['records'] = $query;
        }
        $this->load->view('profile_view', $data);
    }
}

class Profile_model extends CI_Model {
    public function getbank()  # Changed
    {
        $query = $this->db->get('bank');
        return $query->result();  # Changed
    }
}

更新

简单的答案,感谢所有试图提供帮助的人。我的模型文件名带有小写字母而不是大写字母。

Eg profile_model.php
should be Profile_model.php

但是,这个解决方案优雅而简单,因此它改进了我的代码,因此 il 接受这个。

可能是因为您没有加载模型?尝试将控制器更改为:

class Profile extends CI_Controller
{
    function index()
    {
        $data = array();
        $this->load->model('Profile_model');
        if($query = $this->Profile_model->getbank())
        {
            $data['records'] = $query;
        }
        $this->load->view('profile_view', $data);
    }
}