代码点火器调用我的模型的未定义方法时出错



我是codeigniter的新手,我已经阅读了如何用这个框架加载一些模型。现在我在使用我的模型时遇到了一个问题。我有一个错误Message: Call to undefined method User_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();
}
}

最新更新