Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php codeigniter调用模型中未定义的方法时出错_Php_Codeigniter_Model_Codeigniter 3 - Fatal编程技术网

Php codeigniter调用模型中未定义的方法时出错

Php codeigniter调用模型中未定义的方法时出错,php,codeigniter,model,codeigniter-3,Php,Codeigniter,Model,Codeigniter 3,我是codeigniter的新手,我读过如何用这个框架加载一些模型。现在我在使用我的模型时遇到了一个问题。我有一个错误消息:调用未定义的方法User\u model::select() 我的情况很简单,我想实现一个数据表,并通过它使用我的模型。以下是我的错误: //total records foreach ($this->user->select("COUNT(*) AS count", $where, $join) as $key =&

我是codeigniter的新手,我读过如何用这个框架加载一些模型。现在我在使用我的模型时遇到了一个问题。我有一个错误
消息:调用未定义的方法User\u model::select()

我的情况很简单,我想实现一个数据表,并通过它使用我的模型。以下是我的错误:

//total records
            foreach ($this->user->select("COUNT(*) AS count", $where, $join) as $key => $value) {
                $data["iTotalDisplayRecords"] = $value->count;
                $data["iTotalRecords"] = $value->count;
            }
如您所见,我使用的是
$this->user
,它指的是我在构造函数中加载的模型:

public function __construct() {
        parent::__construct();
        if (!$this->ion_auth->logged_in()){
            redirect('auth/login', 'refresh');//redirect them to the login page
        }
        if (!$this->ion_auth->is_admin()){
            redirect(base_url().'login', 'refresh');
        }else{
            $this->admin_id = $this->session->userdata('user_id');
            $this->load->library("pagination");
            $this->load->model('user_model','user');                    
        }
    }
以下是我的模型的代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_model extends CI_Model {
    public function __construct() {
        if ($this->ion_auth->logged_in()){
            $this->user_id = $this->session->userdata('user_id');
            $group = $this->ion_auth->get_users_groups($this->user_id)->result();
            $this->group_id = $group[0]->id;
        }
    }

    public $tbl_name = "users";

}

否这不是在CI3中调用select的方法。您必须在模型类中编写自己的方法,然后调用该方法

我已重写了您的模型,请尝试以下操作:

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class User_model extends CI_Model
{

    private $tbl_name = "users";

    public function __construct()
    {
        if ($this->ion_auth->logged_in()) {
            $this->user_id = $this->session->userdata('user_id');
            $group = $this->ion_auth->get_users_groups($this->user_id)->result();
            $this->group_id = $group[0]->id;
        }
    }

    public function select($select, $where, $join)
    {
        $this->db->select($select);
        $this->db->from($this->$tbl_name);
        $this->db->where($where);
        $this->db->join($join);
        return $this->db->get()->result();
    }
}

如果我的答案有效,请将其标记为有用。非常感谢。