【发布时间】:2016-03-28 01:40:16
【问题描述】:
我创建了一个包含所有 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 { }
?>
应用程序/控制器/users.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->load->model("user_model"); 然后视图页面加载,...无法弄清楚发生了什么...请帮助..
【问题讨论】:
-
我已经在 user_model 中使用了 crud 函数...它工作正常..但是当我将所有 crud 函数放入 my_model 时...它不起作用...my_model 没有在 user_model 中被继承..
标签: php codeigniter