【问题标题】:inheriting a model in codeigniter在 codeigniter 中继承模型
【发布时间】: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


【解决方案1】:

在 CI 配置文件 'application/config/config.php' 中找到并设置配置项

$config['subclass_prefix'] = 'My_';

那么 CI load_class 函数将在您的例程中调用 $ths-&gt;load-&gt;model('user_model') 时加载 CI_ModelMy_model

【讨论】:

  • 它在 windows pc 上工作正常..不知道我的 linux 系统有什么问题
  • 无法弄清楚为什么同样的事情在 linux 中不起作用..请帮助
  • 错误报告已开启..致命错误:第 2 行的 /var/www/html/demo/application/models/user_model.php 中未找到 Class 'My_Model'
  • 可能是因为linux系统是区分大小写的,我答错了,你的类名是My_Model,我写的是MY_,所以用$config['subclass_prefix'] = 'My_';再试一下跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2014-04-04
  • 1970-01-01
相关资源
最近更新 更多