我创建了一个包含所有 crud 函数的自定义模型 (My_Model(。 现在我想在其他模型中继承该通用模型类。
应用程序/内核/My_Model.php
<?php
class My_Model extends CI_Model {
protected $_table;
public function __construct() {
parent::__construct();
$this->load->helper("inflector");
if(!$this->_table){
$this->_table = strtolower(plural(str_replace("_model", "", get_class($this))));
}
}
public function get() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->get($this->_table)->row();
}
public function get_all() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->get($this->_table)->result();
}
public function insert($data) {
$success = $this->db->insert($this->_table, $data);
if($success) {
return $this->db->insert_id();
} else {
return FALSE;
}
}
public function update() {
$args = func_get_args();
if(is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->update($this->_table, $args[1]);
}
public function delete() {
$args = func_get_args();
if(count($args) > 1 || is_array($args[0])) {
$this->db->where($args[0]);
} else {
$this->db->where("id", $args[0]);
}
return $this->db->delete($this->_table);
}
}
?>
应用/型号/user_model.php
<?php
class User_model extends My_Model { }
?>
应用程序/控制器/用户.php
<?php
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model("user_model");
}
function index() {
if($this->input->post("signup")) {
$data = array(
"username" => $this->input->post("username"),
"email" => $this->input->post("email"),
"password" => $this->input->post("password"),
"fullname" => $this->input->post("fullname")
);
if($this->user_model->insert($data)) {
$this->session->set_flashdata("message", "Success!");
redirect(base_url()."users");
}
}
$this->load->view("user_signup");
}
}
?>
当我加载控制器时,我收到 500 内部服务器错误,但如果我取消注释控制器中的行 - $this->加载>模型("user_model"(;然后加载视图页面,...不知道发生了什么...请帮忙..
在 CI 配置文件 'application/config/config.php' 中查找并设置配置项
$config['subclass_prefix'] = 'My_';
然后 CI load_class
函数将在例程中调用 $ths->load->model('user_model')
时加载CI_Model
和My_model
;